Package net.hydromatic.morel.eval
Class Closure
java.lang.Object
net.hydromatic.morel.eval.Closure
- All Implemented Interfaces:
Comparable<Closure>,Applicable,Describable
Value that is sufficient for a function to bind its argument
and evaluate its body.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classCallback forbindRecurse(Core.Pat, Object, BiConsumer)that modifies an environment. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) EvalEnvBinds an argument value to create a new environment for a closure.(package private) ObjectSimilar tobind(java.lang.Object), but also evaluates.static booleanbindRecurse(Core.Pat pat, Object argValue, BiConsumer<Core.NamedPat, Object> envRef) Attempts to bind a value to a pattern.int(package private) EvalEnvSimilar tobind(java.lang.Object), but evaluates an expression first.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface net.hydromatic.morel.eval.Applicable
asCode
-
Field Details
-
evalEnv
Environment for evaluation. Contains the variables "captured" from the environment when the closure was created. -
patCodes
A list of (pattern, code) pairs. During bind, the value being bound is matched against each pattern. When a match is found, the code for that pattern is used to evaluate.For example, when applying
fn x => case x of 0 => "yes" | _ => "no"to the value1, the first pattern (0fails) but the second pattern (_) succeeds, and therefore we evaluate the second code"no". -
pos
-
-
Constructor Details
-
Closure
Not a public API.
-
-
Method Details
-
toString
-
compareTo
- Specified by:
compareToin interfaceComparable<Closure>
-
bind
Binds an argument value to create a new environment for a closure.When calling a simple function such as
(fn x => x + 1) 2, the binder sets just contains one variable,x, and the new environment containsx = 1. If the function's parameter is a match, more variables might be bound. For example, when you invoke(fn (x, y) => x + y) (3, 4), the binder setsxto 3 andyto 4. -
evalBind
Similar tobind(java.lang.Object), but evaluates an expression first. -
bindEval
Similar tobind(java.lang.Object), but also evaluates. -
apply
- Specified by:
applyin interfaceApplicable
-
describe
- Specified by:
describein interfaceDescribable
-
bindRecurse
public static boolean bindRecurse(Core.Pat pat, Object argValue, BiConsumer<Core.NamedPat, Object> envRef) Attempts to bind a value to a pattern. Returns whether it has succeeded in matching the whole pattern.Each time it matches a name, calls a consumer. It's possible that the consumer is called a few times even if the whole pattern ultimately fails to match.
-