hydra.lib.sets.Map Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hydra-java Show documentation
Show all versions of hydra-java Show documentation
The Hydra language for strongly-typed transformations
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());
}
}