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

hydra.Lexical Maven / Gradle / Ivy

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

import hydra.compute.Flow;
import hydra.core.Name;
import hydra.graph.Graph;
import hydra.graph.Primitive;

import hydra.util.Opt;

import static hydra.Flows.bind;
import static hydra.Flows.fail;
import static hydra.Flows.getState;


/**
 * Lexical functions (which deal with named elements and primitives within graphs).
 */
public class Lexical {
    private Lexical() {
    }

    /**
     * Get a primitive from a graph by name; the primitive is not required to exist.
     */
    public static  Opt lookupPrimitive(Graph g, Name name) {
        return Opt.ofNullable(g.primitives.get(name));
    }

    /**
     * Get a primitive from the current graph by name; the primitive is required to exist.
     */
    public static  Flow requirePrimitive(Name name) {
        return bind(getState(), g -> {
            Opt mprim = lookupPrimitive(g, name);
            return mprim.>map(Flows::pure)
                .orElseGet(() -> fail("no such primitive function: " + name.value));
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy