hydra.lib.lists.Cons 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.lists;
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.ArrayList;
import java.util.List;
import java.util.function.Function;
import static hydra.dsl.Types.function;
import static hydra.dsl.Types.lambda;
import static hydra.dsl.Types.list;
public class Cons extends PrimitiveFunction {
public static final Name NAME = new Name("hydra/lib/lists.cons");
public Name name() {
return NAME;
}
@Override
public Type type() {
return lambda("a", function("a", list("a"), list("a")));
}
@Override
protected Function, Flow> implementation() {
return args -> Flows.map(
Expect.list(Flows::pure, args.get(1)),
l -> Terms.list(Cons.apply(args.get(0), l)));
}
/**
* Apply the function to both arguments.
*/
public static List apply(X el, List l) {
List combined = new ArrayList<>();
combined.add(el);
combined.addAll(l);
return combined;
}
}