|
| 1 | +package fj.data; |
| 2 | + |
| 3 | +import fj.F; |
| 4 | +import fj.F0; |
| 5 | +import fj.P; |
| 6 | +import fj.P1; |
| 7 | +import fj.control.Trampoline; |
| 8 | + |
| 9 | +/** |
| 10 | + * <code>Eval</code> is an abstraction over different models of evaluation. |
| 11 | + * The data constructors: |
| 12 | + * <ul> |
| 13 | + * <li><code>Now</code> - the value is evaluated immediately.</li> |
| 14 | + * <li><code>Later</code> - the value is evaluated only once when it's requested (lazy evaluation).</li> |
| 15 | + * <li><code>Always</code> - the value is evaluated every time when it's requested.</li> |
| 16 | + * </ul> |
| 17 | + * |
| 18 | + * Both <code>Later</code> and <code>Always</code> are lazy computations, while <code>Now</code> is eager. |
| 19 | + * |
| 20 | + * |
| 21 | + * @version %build.number% |
| 22 | + */ |
| 23 | +public abstract class Eval<A> { |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructs an eager evaluation by wrapping the given value. |
| 27 | + * |
| 28 | + * @param a the evaluated value. |
| 29 | + * @return an eval with computed value. |
| 30 | + */ |
| 31 | + public static <A> Eval<A> now(A a) { |
| 32 | + return new Now<>(a); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructs a lazy evaluation with caching. |
| 37 | + * |
| 38 | + * @param a the supplier that evaluates a value. |
| 39 | + * @return a lazy evaluation. |
| 40 | + */ |
| 41 | + public static <A> Eval<A> later(F0<A> a) { |
| 42 | + return new Later<>(a); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructs a lazy evaluation without caching. |
| 47 | + * |
| 48 | + * @param a the supplier that evaluates a value. |
| 49 | + * @return a lazy evaluation. |
| 50 | + */ |
| 51 | + public static <A> Eval<A> always(F0<A> a) { |
| 52 | + return new Always<>(a); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructs a lazy evaluation of an expression that produces <code>Eval<A></code>. |
| 57 | + * This operation is stack-safe and can be used for recursive computations. |
| 58 | + * |
| 59 | + * @param a the supplier that produces an <code>Eval<A></code>. |
| 60 | + * @return a lazily evaluated nested evaluation. |
| 61 | + */ |
| 62 | + public static <A> Eval<A> defer(F0<Eval<A>> a) { |
| 63 | + return new DeferEval<>(a); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Evaluates the computation and return its result. |
| 68 | + * Depending on whether the current instance is lazy or eager the |
| 69 | + * computation may or may not happen at this point. |
| 70 | + * |
| 71 | + * @return a result of this computation. |
| 72 | + */ |
| 73 | + public abstract A value(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Transforms <code>Eval<A></code> into a <code>Eval<B></code> using |
| 77 | + * the given function. |
| 78 | + * |
| 79 | + * Note: the computation of the given transformation is always lazy, |
| 80 | + * even if it invoked for an eager <code>Now</code> instance. This computation |
| 81 | + * is performed in O(1) stack space. |
| 82 | + * |
| 83 | + * @param f the transformation function. |
| 84 | + * @return a transformed evaluation. |
| 85 | + */ |
| 86 | + public final <B> Eval<B> map(final F<A, B> f) { |
| 87 | + return bind(a -> now(f.f(a))); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Alias for {@link #bind(F)}. |
| 92 | + */ |
| 93 | + public final <B> Eval<B> flatMap(final F<A, Eval<B>> f) { |
| 94 | + return bind(f); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Transforms <code>Eval<A></code> into a <code>Eval<B></code> using |
| 99 | + * the given function that directly produces <code>Eval<B></code>. |
| 100 | + * |
| 101 | + * Note: the computation of the given transformation is always lazy, |
| 102 | + * even if it invoked for an eager <code>Now</code> instance. This computation |
| 103 | + * is performed in O(1) stack space. |
| 104 | + * |
| 105 | + * @param f the transformation function. |
| 106 | + * @return a transformed evaluation. |
| 107 | + */ |
| 108 | + public final <B> Eval<B> bind(final F<A, Eval<B>> f) { |
| 109 | + return new BindTrampolineEval<>(f, asTrampoline()); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Transforms the current instance into a trampoline instance. |
| 114 | + */ |
| 115 | + abstract TrampolineEval<A> asTrampoline(); |
| 116 | + |
| 117 | + /** |
| 118 | + * Represents an eager computation. |
| 119 | + */ |
| 120 | + private static final class Now<A> extends Eval<A> { |
| 121 | + private final A a; |
| 122 | + |
| 123 | + Now(A a) { |
| 124 | + this.a = a; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public final A value() { |
| 129 | + return a; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + final TrampolineEval<A> asTrampoline() { |
| 134 | + return new PureTrampolineEval<>(this); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Represents a lazy computation that is evaluated only once. |
| 140 | + */ |
| 141 | + private static final class Later<A> extends Eval<A> { |
| 142 | + private final P1<A> memo; |
| 143 | + |
| 144 | + Later(F0<A> producer) { |
| 145 | + this.memo = P.hardMemo(producer); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public final A value() { |
| 150 | + return memo._1(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + final TrampolineEval<A> asTrampoline() { |
| 155 | + return new PureTrampolineEval<>(this); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Represents a lazy computation that is evaluated every time when it's requested. |
| 161 | + */ |
| 162 | + private static final class Always<A> extends Eval<A> { |
| 163 | + private final F0<A> supplier; |
| 164 | + |
| 165 | + Always(F0<A> supplier) { |
| 166 | + this.supplier = supplier; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public final A value() { |
| 171 | + return supplier.f(); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + final TrampolineEval<A> asTrampoline() { |
| 176 | + return new PureTrampolineEval<>(this); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * A helper abstraction that allows to perform recursive lazy transformations in O(1) stack space. |
| 182 | + */ |
| 183 | + private static abstract class TrampolineEval<A> extends Eval<A> { |
| 184 | + |
| 185 | + protected abstract Trampoline<A> trampoline(); |
| 186 | + |
| 187 | + @Override |
| 188 | + public final A value() { |
| 189 | + return trampoline().run(); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + final TrampolineEval<A> asTrampoline() { |
| 194 | + return this; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private static final class PureTrampolineEval<A> extends TrampolineEval<A> { |
| 199 | + private final Eval<A> start; |
| 200 | + |
| 201 | + PureTrampolineEval(Eval<A> start) { |
| 202 | + this.start = start; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + protected final Trampoline<A> trampoline() { |
| 207 | + return Trampoline.suspend(P.lazy(() -> Trampoline.pure(start.value()))); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private static final class BindTrampolineEval<A, B> extends TrampolineEval<B> { |
| 212 | + private final TrampolineEval<A> next; |
| 213 | + private final F<A, Eval<B>> f; |
| 214 | + |
| 215 | + BindTrampolineEval(F<A, Eval<B>> f, TrampolineEval<A> next) { |
| 216 | + this.next = next; |
| 217 | + this.f = f; |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + protected final Trampoline<B> trampoline() { |
| 222 | + return Trampoline.suspend(P.lazy(() -> next.trampoline().bind(v -> f.f(v).asTrampoline().trampoline()))); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + private static final class DeferEval<A> extends TrampolineEval<A> { |
| 227 | + private final P1<Eval<A>> memo; |
| 228 | + |
| 229 | + DeferEval(F0<Eval<A>> producer) { |
| 230 | + this.memo = P.hardMemo(producer); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + protected final Trampoline<A> trampoline() { |
| 235 | + return memo._1().asTrampoline().trampoline(); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments