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

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

There is a newer version: 0.8.0
Show newest version
package hydra.lib.sets;

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.List;
import java.util.function.Function;
import java.util.Set;
import java.util.stream.Collectors;

import static hydra.dsl.Types.function;
import static hydra.dsl.Types.lambda;
import static hydra.dsl.Types.set;


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

    @Override
    public Type type() {
        return lambda("x", lambda("y",
                function(function("x", "y"), set("x"), set("y"))));
    }

    @Override
    protected Function, Flow> implementation() {
        return args -> {
            Term mapping = args.get(0);
            return Flows.map(Expect.set(Flows::pure, args.get(1)),
                arg -> Terms.set(arg.stream().map(e -> Terms.apply(mapping, e)).collect(Collectors.toSet())));
        };
    }

    public static  Function, Set> apply(Function mapping) {
        return (arg) -> apply(mapping, arg);
    }

    public static  Set apply(Function mapping, Set arg) {
        return arg.stream().map(mapping).collect(Collectors.toSet());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy