hydra.Lexical 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;
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));
});
}
}