blob: 76f7b49c917bbc46c7783f6e6853770d07dce894 (
plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
(.module:
[library
[lux "*"
[abstract
[functor {"+" Functor}]
[apply {"+" Apply}]
[monad {"+" Monad do}]
[equivalence {"+" Equivalence}]]
[control
["[0]" io]
[parser
["<[0]>" code]]
[concurrency
["[0]" atom]]]
[macro {"+" with_symbols}
[syntax {"+" syntax:}]]
[type
abstract]]])
(abstract: .public (Lazy a)
(-> [] a)
(def: (lazy' generator)
(All (_ a) (-> (-> [] a) (Lazy a)))
(let [cache (atom.atom {.#None})]
(:abstraction (function (_ _)
(case (io.run! (atom.read! cache))
{.#Some value}
value
_
(let [value (generator [])]
(exec
(io.run! (atom.compare_and_swap! _ {.#Some value} cache))
value)))))))
(def: .public (value lazy)
(All (_ a) (-> (Lazy a) a))
((:representation lazy) [])))
(syntax: .public (lazy [expression <code>.any])
(with_symbols [g!_]
(in (list (` ((~! ..lazy') (function ((~ g!_) (~ g!_)) (~ expression))))))))
(implementation: .public (equivalence (^open "_#[0]"))
(All (_ a) (-> (Equivalence a) (Equivalence (Lazy a))))
(def: (= left right)
(_#= (..value left)
(..value right))))
(implementation: .public functor
(Functor Lazy)
(def: (each f fa)
(lazy (f (value fa)))))
(implementation: .public apply
(Apply Lazy)
(def: &functor ..functor)
(def: (on fa ff)
(lazy ((value ff) (value fa)))))
(implementation: .public monad
(Monad Lazy)
(def: &functor ..functor)
(def: in (|>> lazy))
(def: conjoint ..value))
|