Package fj.data
Class Eval<A>
- java.lang.Object
-
- fj.data.Eval<A>
-
- Direct Known Subclasses:
Eval.Always
,Eval.Later
,Eval.Now
,Eval.TrampolineEval
public abstract class Eval<A> extends java.lang.Object
Eval
is an abstraction over different models of evaluation. The data constructors:Now
- the value is evaluated immediately.Later
- the value is evaluated only once when it's requested (lazy evaluation).Always
- the value is evaluated every time when it's requested.
Later
andAlways
are lazy computations, whileNow
is eager.- Version:
- %build.number%
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Eval.Always<A>
Represents a lazy computation that is evaluated every time when it's requested.private static class
Eval.BindTrampolineEval<A,B>
private static class
Eval.DeferEval<A>
private static class
Eval.Later<A>
Represents a lazy computation that is evaluated only once.private static class
Eval.Now<A>
Represents an eager computation.private static class
Eval.PureTrampolineEval<A>
private static class
Eval.TrampolineEval<A>
A helper abstraction that allows to perform recursive lazy transformations in O(1) stack space.
-
Constructor Summary
Constructors Constructor Description Eval()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static <A> Eval<A>
always(F0<A> a)
Constructs a lazy evaluation without caching.(package private) abstract Eval.TrampolineEval<A>
asTrampoline()
Transforms the current instance into a trampoline instance.<B> Eval<B>
bind(F<A,Eval<B>> f)
static <A> Eval<A>
defer(F0<Eval<A>> a)
<B> Eval<B>
flatMap(F<A,Eval<B>> f)
Alias forbind(F)
.static <A> Eval<A>
later(F0<A> a)
Constructs a lazy evaluation with caching.<B> Eval<B>
map(F<A,B> f)
static <A> Eval<A>
now(A a)
Constructs an eager evaluation by wrapping the given value.abstract A
value()
Evaluates the computation and return its result.
-
-
-
Method Detail
-
now
public static <A> Eval<A> now(A a)
Constructs an eager evaluation by wrapping the given value.- Parameters:
a
- the evaluated value.- Returns:
- an eval with computed value.
-
later
public static <A> Eval<A> later(F0<A> a)
Constructs a lazy evaluation with caching.- Parameters:
a
- the supplier that evaluates a value.- Returns:
- a lazy evaluation.
-
always
public static <A> Eval<A> always(F0<A> a)
Constructs a lazy evaluation without caching.- Parameters:
a
- the supplier that evaluates a value.- Returns:
- a lazy evaluation.
-
value
public abstract A value()
Evaluates the computation and return its result. Depending on whether the current instance is lazy or eager the computation may or may not happen at this point.- Returns:
- a result of this computation.
-
map
public final <B> Eval<B> map(F<A,B> f)
TransformsEval
into aEval
using the given function. Note: the computation of the given transformation is always lazy, even if it invoked for an eagerNow
instance. This computation is performed in O(1) stack space.- Parameters:
f
- the transformation function.- Returns:
- a transformed evaluation.
-
bind
public final <B> Eval<B> bind(F<A,Eval<B>> f)
TransformsEval
into aEval
using the given function that directly producesEval
. Note: the computation of the given transformation is always lazy, even if it invoked for an eagerNow
instance. This computation is performed in O(1) stack space.- Parameters:
f
- the transformation function.- Returns:
- a transformed evaluation.
-
asTrampoline
abstract Eval.TrampolineEval<A> asTrampoline()
Transforms the current instance into a trampoline instance.
-
-