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

hydra.lib.lists.Bind Maven / Gradle / Ivy

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

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


public class Bind extends PrimitiveFunction {
    public Name name() {
        return new Name("hydra/lib/lists.bind");
    }

    @Override
    public Type type() {
        return lambda("a", lambda("b",
            function(list("a"), function("a", list("b")), list("b"))));
    }

    @Override
    protected Function>, Flow, Term>> implementation() {
        return args -> Flows.map(Expect.list(Flows::pure, args.get(0)), argsArg -> {
            Term mapping = args.get(1);
            return Terms.apply(
                Terms.primitive(Concat.NAME),
                Terms.list(argsArg.stream().map(a -> Terms.apply(mapping, a)).collect(Collectors.toList())));
        });
    }

    public static  Function>, List> apply(List args) {
        return (mapping) -> apply(args, mapping);
    }

    public static  List apply(List args, Function> mapping) {
        return args.stream().flatMap(x -> mapping.apply(x).stream()).collect(Collectors.toList());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy