blob: 677832ebdcfe4d5dc3dd55f68e8045fb072e18cc (
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
71
72
73
74
|
(.module:
[library
[lux #*
[abstract
[functor (#+ Functor)]
[apply (#+ Apply)]
[monad (#+ Monad do)]
[equivalence (#+ Equivalence)]]
[control
["." io]
[parser
["s" code]]
[concurrency
["." atom]]]
[macro (#+ with_identifiers)
[syntax (#+ syntax:)]]
[type
abstract]]])
(abstract: .public (Lazy a)
{#.doc (example "A value specified by an expression that is calculated only at the last moment possible."
"Afterwards, the value is cached for future reference.")}
(-> [] 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)
{#.doc (example "Specifies a lazy value by providing the expression that computes it.")}
(with_identifiers [g!_]
(in (list (` ((~! lazy') (function ((~ g!_) (~ g!_)) (~ expression))))))))
(implementation: .public (equivalence (^open "\."))
(All [a] (-> (Equivalence a) (Equivalence (Lazy a))))
(def: (= left right)
(\= (..value left)
(..value right))))
(implementation: .public functor
(Functor Lazy)
(def: (map f fa)
(lazy (f (value fa)))))
(implementation: .public apply
(Apply Lazy)
(def: &functor ..functor)
(def: (apply ff fa)
(lazy ((value ff) (value fa)))))
(implementation: .public monad
(Monad Lazy)
(def: &functor ..functor)
(def: in (|>> lazy))
(def: join value))
|