diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/control/thunk.lux | 33 | ||||
-rw-r--r-- | stdlib/source/lux/data/lazy.lux | 34 |
2 files changed, 34 insertions, 33 deletions
diff --git a/stdlib/source/lux/control/thunk.lux b/stdlib/source/lux/control/thunk.lux deleted file mode 100644 index a78f78023..000000000 --- a/stdlib/source/lux/control/thunk.lux +++ /dev/null @@ -1,33 +0,0 @@ -(;module: - lux - (lux [io] - (control monad) - (concurrency ["A" atom]) - [macro] - (macro ["s" syntax #+ syntax:]))) - -(type: #export (Thunk a) - (-> [] a)) - -(def: #hidden (freeze' generator) - (All [a] (-> (-> [] a) (-> [] a))) - (let [cache (: (A;Atom (Maybe ($ +0))) - (A;atom #;None))] - (function [_] - (case (io;run (A;get cache)) - (#;Some value) - value - - _ - (let [value (generator [])] - (exec (io;run (A;compare-and-swap _ (#;Some value) cache)) - value)))))) - -(syntax: #export (freeze expr) - (do @ - [g!arg (macro;gensym "")] - (wrap (list (` (freeze' (function [(~ g!arg)] (~ expr)))))))) - -(def: #export (thaw thunk) - (All [a] (-> (Thunk a) a)) - (thunk [])) diff --git a/stdlib/source/lux/data/lazy.lux b/stdlib/source/lux/data/lazy.lux new file mode 100644 index 000000000..1f4566eee --- /dev/null +++ b/stdlib/source/lux/data/lazy.lux @@ -0,0 +1,34 @@ +(;module: + lux + (lux [io] + (control monad) + (concurrency ["A" atom]) + [macro] + (macro ["s" syntax #+ syntax:]) + (type model))) + +(model: #export (Lazy a) + (-> [] a) + + (def: #hidden (freeze' generator) + (All [a] (-> (-> [] a) (Lazy a))) + (let [cache (: (A;Atom (Maybe ($ +0))) + (A;atom #;None))] + (@model (function [_] + (case (io;run (A;get cache)) + (#;Some value) + value + + _ + (let [value (generator [])] + (exec (io;run (A;compare-and-swap _ (#;Some value) cache)) + value))))))) + + (def: #export (thaw l-value) + (All [a] (-> (Lazy a) a)) + ((@repr l-value) []))) + +(syntax: #export (freeze expr) + (do @ + [g!_ (macro;gensym "_")] + (wrap (list (` (freeze' (function [(~ g!_)] (~ expr)))))))) |