blob: 044a1eb70c7bb3b78c28971106ce620b6eedf6b2 (
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
|
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
(.require
[library
[lux (.except except)
[abstract
[functor (.only Functor)]
[apply (.only Apply)]
["[0]" monad (.only Monad do)]]
[control
["[0]" try (.only Try)]]
[data
["[0]" text (.only)
["%" \\format (.only format)]]
[collection
["[0]" list (.use "[1]#[0]" mix)]]]]]
[//
["[0]" exception (.only Exception)]])
(type (Cleaner r !)
(-> r (! (Try Any))))
(type .public (Region r ! a)
(-> [r (List (Cleaner r !))]
(! [(List (Cleaner r !))
(Try a)])))
(def separator
Text
(format text.new_line
"-----------------------------------------" text.new_line
"-----------------------------------------" text.new_line
"-----------------------------------------" text.new_line
text.new_line))
(exception.def .public (clean_up_error [error output])
(All (_ a) (Exception [Text (Try a)]))
(format error
(when output
{try.#Success _}
""
{try.#Failure error|output}
(format separator
error|output))))
(def (clean clean_up output)
(All (_ a) (-> (Try Any) (Try a) (Try a)))
(when clean_up
{try.#Success _}
output
{try.#Failure error}
(exception.except ..clean_up_error [error output])))
(def .public (run! monad computation)
(All (_ ! a)
(-> (Monad !) (All (_ r) (Region r ! a))
(! (Try a))))
(do [! monad]
[[cleaners output] (computation [[] (list)])]
(|> cleaners
(monad.each ! (function (_ cleaner) (cleaner [])))
(of ! each (list#mix clean output)))))
(def .public (acquire! monad cleaner value)
(All (_ ! a) (-> (Monad !) (-> a (! (Try Any))) a
(All (_ r) (Region r ! a))))
(function (_ [region cleaners])
(of monad in [{.#Item (function (_ region) (cleaner value))
cleaners}
{try.#Success value}])))
(def .public (functor super)
(All (_ !)
(-> (Functor !)
(All (_ r) (Functor (Region r !)))))
(implementation
(def (each f)
(function (_ fa)
(function (_ region+cleaners)
(of super each
(function (_ [cleaners' temp])
[cleaners' (when temp
{try.#Success value}
{try.#Success (f value)}
{try.#Failure error}
{try.#Failure error})])
(fa region+cleaners)))))))
(def .public (apply super)
(All (_ !)
(-> (Monad !)
(All (_ r) (Apply (Region r !)))))
(implementation
(def functor
(..functor (the monad.functor super)))
(def (on fa ff)
(function (_ [region cleaners])
(do super
[[cleaners ef] (ff [region cleaners])
[cleaners ea] (fa [region cleaners])]
(when ef
{try.#Success f}
(when ea
{try.#Success a}
(in [cleaners {try.#Success (f a)}])
{try.#Failure error}
(in [cleaners {try.#Failure error}]))
{try.#Failure error}
(in [cleaners {try.#Failure error}])))))))
(def .public (monad super)
(All (_ !)
(-> (Monad !)
(All (_ r) (Monad (Region r !)))))
(implementation
(def functor
(..functor (the monad.functor super)))
(def (in value)
(function (_ [region cleaners])
(of super in [cleaners {try.#Success value}])))
(def (conjoint ffa)
(function (_ [region cleaners])
(do super
[[cleaners efa] (ffa [region cleaners])]
(when efa
{try.#Success fa}
(fa [region cleaners])
{try.#Failure error}
(in [cleaners {try.#Failure error}])))))))
(def .public (failure monad error)
(All (_ ! a)
(-> (Monad !) Text
(All (_ r) (Region r ! a))))
(function (_ [region cleaners])
(of monad in [cleaners {try.#Failure error}])))
(def .public (except monad exception message)
(All (_ ! e a)
(-> (Monad !) (Exception e) e
(All (_ r) (Region r ! a))))
(failure monad (exception.error exception message)))
(def .public (lifted monad operation)
(All (_ ! a)
(-> (Monad !) (! a)
(All (_ r) (Region r ! a))))
(function (_ [region cleaners])
(of monad each
(|>> {try.#Success} [cleaners])
operation)))
|