aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--stdlib/source/lux/control/thunk.lux33
-rw-r--r--stdlib/source/lux/data/lazy.lux34
-rw-r--r--stdlib/test/test/lux/control/thunk.lux23
-rw-r--r--stdlib/test/test/lux/data/lazy.lux23
-rw-r--r--stdlib/test/tests.lux2
5 files changed, 58 insertions, 57 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))))))))
diff --git a/stdlib/test/test/lux/control/thunk.lux b/stdlib/test/test/lux/control/thunk.lux
deleted file mode 100644
index 878cf4144..000000000
--- a/stdlib/test/test/lux/control/thunk.lux
+++ /dev/null
@@ -1,23 +0,0 @@
-(;module:
- lux
- (lux [io]
- (control ["M" monad #+ do Monad]
- ["&" thunk])
- ["R" math/random])
- lux/test)
-
-(context: "Thunks"
- [left R;nat
- right R;nat
- #let [thunk (&;freeze (n.* left right))
- expected (n.* left right)]]
- ($_ seq
- (test "Thunking does not alter the expected value."
- (n.= expected
- (&;thaw thunk)))
- (test "Thunks only evaluate once."
- (and (not (is expected
- (&;thaw thunk)))
- (is (&;thaw thunk)
- (&;thaw thunk))))
- ))
diff --git a/stdlib/test/test/lux/data/lazy.lux b/stdlib/test/test/lux/data/lazy.lux
new file mode 100644
index 000000000..bf395bcfa
--- /dev/null
+++ b/stdlib/test/test/lux/data/lazy.lux
@@ -0,0 +1,23 @@
+(;module:
+ lux
+ (lux [io]
+ (control ["M" monad #+ do Monad])
+ (data ["&" lazy])
+ ["R" math/random])
+ lux/test)
+
+(context: "Lazy."
+ [left R;nat
+ right R;nat
+ #let [lazy (&;freeze (n.* left right))
+ expected (n.* left right)]]
+ ($_ seq
+ (test "Lazying does not alter the expected value."
+ (n.= expected
+ (&;thaw lazy)))
+ (test "Lazy values only evaluate once."
+ (and (not (is expected
+ (: Nat (&;thaw lazy))))
+ (is (&;thaw lazy)
+ (&;thaw lazy))))
+ ))
diff --git a/stdlib/test/tests.lux b/stdlib/test/tests.lux
index c423502b7..a97229a10 100644
--- a/stdlib/test/tests.lux
+++ b/stdlib/test/tests.lux
@@ -22,7 +22,6 @@
["_;" cont]
["_;" reader]
["_;" state]
- ["_;" thunk]
["_;" parser])
(data ["_;" bit]
["_;" bool]
@@ -35,6 +34,7 @@
["_;" product]
["_;" sum]
["_;" text]
+ ["_;" lazy]
(number ["_;" ratio]
["_;" complex])
(format ["_;" json]