blob: 1a1fae8561091dfae743a003d836366293dd78c9 (
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
(.require
[library
[lux (.except with when)
[abstract
[apply (.only Apply)]
[equivalence (.only Equivalence)]
["[0]" functor (.only Functor)]
["[0]" monad (.only Monad do)]]
[meta
["@" target]
["[0]" location]]]])
(type .public (Try a)
(Variant
{#Failure Text}
{#Success a}))
(def .public functor
(Functor Try)
(implementation
(def (each f ma)
(.when ma
{#Success datum}
{#Success (f datum)}
... {#Failure msg}
it
(as_expected it)))))
(def .public apply
(Apply Try)
(implementation
(def functor ..functor)
(def (on fa ff)
(.when ff
{#Success f}
(.when fa
{#Success a}
{#Success (f a)}
... {#Failure msg}
it
(as_expected it))
... {#Failure msg}
it
(as_expected it)))))
(def .public monad
(Monad Try)
(implementation
(def functor ..functor)
(def (in a)
{#Success a})
(def (conjoint mma)
(.when mma
{#Success ma}
ma
... {#Failure msg}
it
(as_expected it)))))
(def .public (with monad)
... TODO: Replace (All (_ a) (! (Try a))) with (functor.Then ! Try)
(All (_ !) (-> (Monad !) (Monad (All (_ a) (! (Try a))))))
(implementation
(def functor
(functor.composite (the monad.functor monad)
..functor))
(def in
(|>> (at ..monad in)
(at monad in)))
(def (conjoint MeMea)
(do monad
[eMea MeMea]
(.when eMea
{#Success Mea}
Mea
... {#Failure error}
it
(in (as_expected it)))))))
(def .public (lifted monad)
(All (_ ! a) (-> (Monad !) (-> (! a) (! (Try a)))))
(at monad each (at ..monad in)))
(def .public (equivalence (open "_#[0]"))
(All (_ a) (-> (Equivalence a) (Equivalence (Try a))))
(implementation
(def (= reference sample)
(.when [reference sample]
[{#Success reference} {#Success sample}]
(_#= reference sample)
[{#Failure reference} {#Failure sample}]
(.text_=# reference sample)
_
false
))))
(def .public (trusted try)
(All (_ a)
(-> (Try a) a))
(.when try
{#Success value}
value
{#Failure message}
(panic! message)))
(def .public (maybe try)
(All (_ a)
(-> (Try a) (Maybe a)))
(.when try
{#Success value}
{.#Some value}
... {#Failure message}
_
{.#None}))
(def .public (of_maybe maybe)
(All (_ a)
(-> (Maybe a) (Try a)))
(.when maybe
{.#Some value}
{#Success value}
{.#None}
{#Failure (`` ((.in_module# (,, (static .prelude)) .symbol#encoded) (symbol ..of_maybe)))}))
(def generated_symbol
(macro (_ tokens compiler)
(.when tokens
(list [_ {.#Text prefix}])
(let [generated_symbol (`` (.in_module# (,, (static .prelude)) .generated_symbol))]
(.when (generated_symbol prefix compiler)
{#Success [compiler g!_]}
{#Success [compiler (list g!_)]}
{#Failure error}
{#Failure error}))
_
(undefined))))
(def .public else
(with_expansions [g!then (generated_symbol "g!then")
g!failure (generated_symbol "g!failure")]
(template (_ <else> <try>)
[(.when <try>
{..#Success g!then}
g!then
... {..#Failure g!failure}
g!failure
<else>)])))
(def .public when
(template (_ <test> <then>)
[(if <test>
<then>
{..#Failure (let [symbol#encoded (`` (.in_module# (,, (static .prelude)) .symbol#encoded))]
(all .text_composite#
"[" (symbol#encoded (symbol ..when)) "]"
" " "Invalid condition!"))})]))
|