aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/coffee/liz/lambda/eval/Thunk.java
blob: fdbda93009b0044e6234f7abd67a6716f5ac6d3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package coffee.liz.lambda.eval;

import lombok.RequiredArgsConstructor;

import java.util.function.Supplier;

/**
 * A memoizing thunk for lazy evaluation.
 * 
 * @param <T>
 *            Thunk type
 */
@RequiredArgsConstructor
public final class Thunk<T> implements Supplier<T> {
    private final Supplier<T> thinker; // https://www.youtube.com/shorts/Dzksib8YxSY
    private T cached = null;
    private boolean evaluated = false;

    @Override
    public T get() {
        if (!evaluated) {
            cached = thinker.get();
            evaluated = true;
        }
        return cached;
    }
}