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

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

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

import hydra.Flows;
import hydra.compute.Flow;
import hydra.compute.FlowState;
import hydra.core.Name;
import hydra.core.OptionalCases;
import hydra.core.Term;
import hydra.core.Type;
import hydra.dsl.Types;
import hydra.graph.Graph;
import hydra.tools.PrimitiveFunction;

import java.util.List;
import java.util.function.Function;

import static hydra.dsl.Terms.app;
import static hydra.dsl.Terms.flowState;
import static hydra.dsl.Terms.flowStateState;
import static hydra.dsl.Terms.flowStateTrace;
import static hydra.dsl.Terms.foldOpt;
import static hydra.dsl.Terms.lambda;
import static hydra.dsl.Terms.nothing;
import static hydra.dsl.Terms.project;
import static hydra.dsl.Terms.variable;


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

    @Override
    public Type type() {
        return Types.lambda("s", "x", "y",
                Types.function(
                        Types.flow("s", "x"),
                        Types.function("x", Types.flow("s", "y")),
                        Types.flow("s", "y")));
    }

    @Override
    protected Function, Flow> implementation() {
        return args -> {
            Term input = args.get(0);
            Term mapping = args.get(1);
            Term ifNothing = flowState(nothing(),
                    app("fs1", flowStateState()), app("fs1", flowStateTrace()));
            Term ifJust = lambda("x",
                    app(mapping, variable("x"), app("fs1", flowStateState()), app("fs2", flowStateTrace())));
            Term output = lambda("s0", "t0", app(
                    lambda("fs1", app(foldOpt(
                            new OptionalCases(ifNothing, ifJust)),
                            project(FlowState.NAME, "value"))),
                    app(input, variable("s0"), variable("s1"))));
            return Flows.pure(output);
        };
    }

    public static  Function>, Flow> apply(Flow input) {
        return mapping -> apply(input, mapping);
    }

    public static  Flow apply(Flow input, Function> mapping) {
        return Flows.bind(input, mapping);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy