This file implements frozen values in Javascript. In a Typescript-like manner :
type Lazy<A> = Frozen<A> | Evaluated<A>type Frozen<A> = { func: () => A }type Evaluated<A> = { func: () => A, result: A } Copy
type Lazy<A> = Frozen<A> | Evaluated<A>type Frozen<A> = { func: () => A }type Evaluated<A> = { func: () => A, result: A }
Note that this implementation contains side-effects when thawing values, but preserves the invariant that the computed value is always the same.
import * as L from "./src/utils/lazy.js";const aVal = L.freeze(() => 3 + 5); // builds a frozen valueanyToString(aVal); // -> <Frozen>L.thaw(aVal); // computes the frozen valueanyToString(aVal); // -> <Evaluated(8)>anyToString(L.value(aVal)); // -> 8 Copy
import * as L from "./src/utils/lazy.js";const aVal = L.freeze(() => 3 + 5); // builds a frozen valueanyToString(aVal); // -> <Frozen>L.thaw(aVal); // computes the frozen valueanyToString(aVal); // -> <Evaluated(8)>anyToString(L.value(aVal)); // -> 8
This file implements frozen values in Javascript. In a Typescript-like manner :
Note that this implementation contains side-effects when thawing values, but preserves the invariant that the computed value is always the same.
Example