-
Notifications
You must be signed in to change notification settings - Fork 9
/
either.nim
385 lines (316 loc) · 10.5 KB
/
either.nim
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import sugar,
boost/types,
classy,
./list,
./option,
./kleisli,
macros,
./function
{.experimental.}
type
EitherKind = enum
ekLeft, ekRight
Either*[E,A] = ref object
## Either ADT
case kind: EitherKind
of ekLeft:
lValue: E
else:
rValue: A
EitherE*[A] = Either[ref Exception, A]
EitherS*[A] = Either[string, A]
proc Left*[E,A](value: E): Either[E,A] =
## Constructs left value
Either[E,A](kind: ekLeft, lValue: value)
proc Right*[E,A](value: A): Either[E,A] =
## Constructs right value
Either[E,A](kind: ekRight, rValue: value)
proc left*[E,A](value: E, d: A): Either[E,A] =
## Constructs left value
Left[E,A](value)
proc left*[E](value: E, A: typedesc): Either[E,A] =
## Constructs left value
Left[E,A](value)
proc right*[E,A](value: A, d: E): Either[E,A] =
## Constructs right value
Right[E,A](value)
proc right*[A](value: A, E: typedesc): Either[E,A] =
## Constructs right value
Right[E,A](value)
proc rightE*[A](value: A): EitherE[A] =
## Constructs right value
Right[ref Exception,A](value)
proc rightS*[A](value: A): EitherS[A] =
## Constructs right value
Right[string,A](value)
proc isLeft*[E,A](e: Either[E,A]): bool =
## Checks if `e` contains left value
e.kind == ekLeft
proc isRight*[E,A](e: Either[E,A]): bool =
## Checks if `e` contains right value
e.kind == ekRight
proc `$`(e: ref Exception): string =
e.msg
proc errorMsg*[A](e: EitherE[A]): string =
## Returns error message or empty string
if e.isLeft:
e.lValue.msg
else:
""
proc errorMsg*[A](e: EitherS[A]): string =
## Returns error message or empty string
if e.isLeft:
e.lValue
else:
""
proc `$`*[E,A](e: Either[E,A]): string =
## Returns string representation of `e`
if e.isLeft:
"Left(" & $e.lValue & ")"
else:
"Right(" & $e.rValue & ")"
proc `==`*[E,A](x, y: Either[E,A]): bool =
## Compares two values
let r = (x.isLeft, y.isLeft)
if r == (true, true):
x.lValue == y.lValue
elif r == (false, false):
x.rValue == y.rValue
else:
false
proc map*[E,A,B](e: Either[E,A], f: A -> B): Either[E,B] =
## Maps right value of `e` via `f` or returns left value
if e.isLeft: e.lValue.left(B) else: f(e.rValue).right(E)
proc mapLeft*[E,F,A](e: Either[E,A], f: E -> F): Either[F,A] =
## Maps left value of `e` via `f` or returns right value
if e.isRight: e.rValue.right(F) else: f(e.lValue).left(A)
proc flatMap*[E,A,B](e: Either[E,A], f: A -> Either[E,B]): Either[E,B] =
## Returns the result of applying `f` to the right value or returns left value
if e.isLeft: e.lValue.left(B) else: f(e.rValue)
proc get*[E,A](e: Either[E,A]): A =
## Returns either's value if it is right, or fail
doAssert(e.isRight, "Can't get Either's value")
e.rValue
proc getLeft*[E,A](e: Either[E,A]): E =
## Returns either's value if it is left, or fail
doAssert(e.isLeft, "Can't get Either's left value")
e.lValue
proc getOrElse*[E,A](e: Either[E,A], d: A): A =
## Return right value or `d`
if e.isRight: e.rValue else: d
proc getOrElse*[E,A](e: Either[E,A], f: void -> A): A =
## Return right value or result of `f`
if e.isRight: e.rValue else: f()
proc orElse*[E,A](e: Either[E,A], d: Either[E,A]): Either[E,A] =
## Returns `e` if it contains right value, or `d`
if e.isRight: e else: d
proc orElse*[E,A](e: Either[E,A], f: void -> Either[E,A]): Either[E,A] =
## Returns `e` if it contains right value, or the result of `f()`
if e.isRight: e else: f()
proc map2*[E,A,B,C](a: Either[E,A], b: Either[E,B], f: (A, B) -> C): Either[E,C] =
## Maps 2 `Either` values via `f`
a.flatMap((a: A) => b.map((b: B) => f(a,b)))
proc map2F*[A, B, C, E](
ma: Either[E, A],
mb: () -> Either[E, B],
f: (A, B) -> C
): Either[E, C] =
## Maps 2 `Either` values via `f`. Lazy in second argument.
ma.flatMap((a: A) => mb().map((b: B) => f(a, b)))
proc join*[E,A](e: Either[E, Either[E,A]]): Either[E,A] =
## Flattens Either's value
e.flatMap(id)
when compiles(getCurrentException()):
proc tryE*[A](f: () -> A): EitherE[A] =
## Transforms exception to EitherE type
(try: f().rightE except: getCurrentException().left(A))
proc flatTryE*[A](f: () -> EitherE[A]): EitherE[A] =
## Transforms exception to EitherE type
(try: f() except: getCurrentException().left(A))
template tryETImpl(body: typed): untyped =
when type(body) is EitherE:
flatTryE do() -> auto:
body
else:
tryE do() -> auto:
body
macro tryET*(body: untyped): untyped =
## Combination of flatTryS and tryS
var b = if body.kind == nnkDo: body[^1] else: body
result = quote do:
tryETImpl((block:
`b`
))
proc tryS*[A](f: () -> A): EitherS[A] =
## Transforms exception to EitherS type
(try: f().rightS except: getCurrentExceptionMsg().left(A))
proc flatTryS*[A](f: () -> EitherS[A]): EitherS[A] =
## Transforms exception to EitherS type
(try: f() except: getCurrentExceptionMsg().left(A))
template trySTImpl(body: untyped): untyped =
when type(body) is EitherS:
flatTryS do() -> auto:
body
else:
tryS do() -> auto:
body
macro tryST*(body: untyped): untyped =
## Combination of flatTryS and tryS
var b = if body.kind == nnkDo: body[^1] else: body
result = quote do:
trySTImpl((block:
`b`
))
proc run*[E,A](e: Either[E,A]): A =
## Returns right value or raises the error contained
## in the left part
if e.isRight:
result = e.get
else:
when E is ref Exception:
raise e.getLeft
else:
raise newException(Exception, $e.getLeft)
proc fold*[E,A,B](v: Either[E, A], ifLeft: E -> B, ifRight: A -> B): B =
## Applies `ifLeft` if `v` is left, or `ifRight` if `v` is right
if v.isLeft:
ifLeft(v.lValue)
else:
ifRight(v.rValue)
proc traverse*[E, A, B](xs: List[A], f: A -> Either[E, B]): Either[E, List[B]] =
## Transforms the list of `A` into the list of `B` f via `f` only if
## all results of applying `f` are `Right`.
## Doesnt execute `f` for elements after the first `Left` is encountered.
# Implementation with foldRightF breaks semcheck when inferring
# gcsafe. So we have to keep this basic.
# Also, since tail calls are not guaranteed, we use a loop instead
# of recursion.
var rest = xs
var acc = Nil[B]()
while not rest.isEmpty:
let headRes = f(rest.head)
if headRes.isLeft:
return headRes.getLeft.left(List[B])
acc = Cons(headRes.get, acc)
rest = rest.tail
acc.reverse.right(E)
proc sequence*[E,A](xs: List[Either[E,A]]): Either[E,List[A]] =
xs.traverse((x: Either[E,A]) => x)
proc traverseU*[E,A,B](xs: List[A], f: A -> Either[E,B]): Either[E,Unit] =
var rest = xs
while not rest.isEmpty:
let headRes = f(rest.head)
if headRes.isLeft:
return headRes.getLeft.left(Unit)
rest = rest.tail
().right(E)
proc sequenceU*[E,A](xs: List[Either[E,A]]): Either[E,Unit] =
xs.traverseU((x: Either[E,A]) => x)
proc traverse*[E, A, B](
opt: Option[A],
f: A -> Either[E, B]
): Either[E, Option[B]] =
if opt.isEmpty:
B.none.right(E)
else:
f(opt.get).map((b: B) => b.some)
proc sequence*[E, A](oea: Option[Either[E, A]]): Either[E, Option[A]] =
oea.traverse((ea: Either[E, A]) => ea)
proc traverseU*[E, A, B](
opt: Option[A],
f: A -> Either[E, B]
): Either[E, Unit] =
if opt.isEmpty:
().right(E)
else:
f(opt.get).fold(
(e: E) => e.left(Unit),
(v: B) => ().right(E)
)
proc sequenceU*[E, A](oea: Option[Either[E, A]]): Either[E, Unit] =
oea.traverseU((ea: Either[E, A]) => ea)
proc forEach*[E,A](a: Either[E,A], f: A -> void): void =
## Applies `f` to the Either's value if it's right
if a.isRight:
f(a.get)
proc cond*[E,A](flag: bool, a: A, e: E): Either[E,A] =
## If the condition is satisfied, returns a else returns e
if flag: a.right(E) else: e.left(A)
proc condF*[E,A](flag: bool, a: () -> A, e: () -> E): Either[E,A] =
## If the condition is satisfied, returns a else returns e
if flag: a().right(E) else: e().left(A)
proc asEither*[E,A](o: Option[A], e: E): Either[E,A] =
## Converts Option to Either type
condF(o.isDefined, () => o.get, () => e)
proc asEitherF*[E,A](o: Option[A], e: () -> E): Either[E,A] =
## Converts Option to Either type
condF(o.isDefined, () => o.get, e())
proc asOption*[E,A](e: Either[E,A]): Option[A] =
## Converts Either to Option type
if e.isRight: e.get.some
else: A.none
proc flip*[E,A](e: Either[E,A]): Either[A,E] =
## Flips Either's left and right parts
if e.isRight: e.get.left(E)
else: e.getLeft.right(A)
proc whenF*[E](flag: bool, body: () -> Either[E, Unit]): Either[E, Unit] =
## Executes `body` if `flag` is true
if flag: body()
else: ().right(E)
proc whileM*[E,A](a: A, cond: A -> Either[E, bool], body: A -> Either[E, A]): Either[E,A] =
## Executes the body while `cond` returns ``true.right(E)``
var acc = a
while true:
let condRes = cond(acc)
if condRes.isLeft:
return condRes.getLeft.left(A)
elif not condRes.get:
return acc.right(E)
result = body(acc)
if result.isLeft:
return
acc = result.get
proc whileM*[E](cond: () -> Either[E, bool], body: () -> Either[E, Unit]): Either[E, Unit] =
## Executes the body while `cond` returns ``true.right(E)``
whileM[E, Unit]((), _ => cond(), _ => body())
proc toUnit*[E,A](e: Either[E,A]): Either[E, Unit] =
## Discards the Either's value
e.flatMap((_:A) => ().right(E))
proc bracket*[E,A,B](
acquire: () -> Either[E,A],
release: A -> Either[E, Unit],
body: A -> Either[E,B]
): Either[E,B] =
## Acquires the resource with `acquire`, then executes `body`
## and then releases it with `release`.
acquire().flatMap do (a: A) -> auto:
let r = body(a)
release(a).flatMap((_: Unit) => r)
proc catch*[E1,E2,A](
body: Either[E1,A],
handler: E1 -> Either[E2,A]
): Either[E2,A] =
## Runs `body`. If it fails, execute `handler` with the
## value of exception
if body.isLeft:
handler(body.getLeft)
else:
body.get.right(E2)
proc asEitherS*[E,A](e: Either[E,A]): EitherS[A] =
## Converts Either to EitherS
e.mapLeft((err: E) => $err)
proc asEitherE*[E,A](e: Either[E,A]): EitherE[A] =
## Converts Either to EitherE
e.mapLeft((err: E) => newException(Exception, $err))
proc asList*[E,A](e: Either[E,A]): List[A] =
## Converts Either to List
if e.isLeft:
Nil[A]()
else:
asList(e.get)
template elemType*(v: Either): typedesc =
## Part of ``do notation`` contract
type(v.get)
proc point*[E,A](v: A, e: typedesc[Either[E,A]]): Either[E,A] =
v.right(E)
instance KleisliInst, E => Either[E,_], exporting(_)