All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hydra.lib.maps.Map Maven / Gradle / Ivy

package hydra.lib.maps;

import hydra.Flows;
import hydra.compute.Flow;
import hydra.core.Name;
import hydra.core.Term;
import hydra.core.Type;
import hydra.dsl.Expect;
import hydra.dsl.Terms;
import hydra.graph.Graph;
import hydra.tools.PrimitiveFunction;
import java.util.HashMap;
import java.util.List;
import java.util.function.Function;

import static hydra.Flows.pure;
import static hydra.dsl.Types.function;
import static hydra.dsl.Types.lambda;
import static hydra.dsl.Types.map;


public class Map extends PrimitiveFunction {
    public Name name() {
        return new Name("hydra/lib/maps.map");
    }

    @Override
    public Type type() {
        return lambda("k", lambda("v1", lambda("v2",
            function(function("v1", "v2"), map("k", "v1"), map("k", "v2")))));
    }

    @Override
    protected Function>, Flow, Term>> implementation() {
        return args -> Flows.map(
            Expect.map(Flows::pure, v -> pure(Terms.apply(args.get(0), v)), args.get(1)), Terms::map);
    }

    public static  Function, java.util.Map> apply(Function mapping) {
        return (arg) -> apply(mapping, arg);
    }

    /**
     * Apply the function to both arguments.
     */
    public static  java.util.Map apply(Function mapping, java.util.Map arg) {
        java.util.Map result = new HashMap<>();
        for (java.util.Map.Entry e : arg.entrySet()) {
            result.put(e.getKey(), mapping.apply(e.getValue()));
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy