-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
result.gleam
482 lines (460 loc) Β· 9.37 KB
/
result.gleam
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//// Result represents the result of something that may succeed or not.
//// `Ok` means it was successful, `Error` means it was not successful.
import gleam/list
/// Checks whether the result is an `Ok` value.
///
/// ## Examples
///
/// ```gleam
/// > is_ok(Ok(1))
/// True
/// ```
///
/// ```gleam
/// > is_ok(Error(Nil))
/// False
/// ```
///
pub fn is_ok(result: Result(a, e)) -> Bool {
case result {
Error(_) -> False
Ok(_) -> True
}
}
/// Checks whether the result is an `Error` value.
///
/// ## Examples
///
/// ```gleam
/// > is_error(Ok(1))
/// False
/// ```
///
/// ```gleam
/// > is_error(Error(Nil))
/// True
/// ```
///
pub fn is_error(result: Result(a, e)) -> Bool {
case result {
Ok(_) -> False
Error(_) -> True
}
}
/// Updates a value held within the `Ok` of a result by calling a given function
/// on it.
///
/// If the result is an `Error` rather than `Ok` the function is not called and the
/// result stays the same.
///
/// ## Examples
///
/// ```gleam
/// > map(over: Ok(1), with: fn(x) { x + 1 })
/// Ok(2)
/// ```
///
/// ```gleam
/// > map(over: Error(1), with: fn(x) { x + 1 })
/// Error(1)
/// ```
///
pub fn map(over result: Result(a, e), with fun: fn(a) -> b) -> Result(b, e) {
case result {
Ok(x) -> Ok(fun(x))
Error(e) -> Error(e)
}
}
/// Updates a value held within the `Error` of a result by calling a given function
/// on it.
///
/// If the result is `Ok` rather than `Error` the function is not called and the
/// result stays the same.
///
/// ## Examples
///
/// ```gleam
/// > map_error(over: Error(1), with: fn(x) { x + 1 })
/// Error(2)
/// ```
///
/// ```gleam
/// > map_error(over: Ok(1), with: fn(x) { x + 1 })
/// Ok(1)
/// ```
///
pub fn map_error(
over result: Result(a, e),
with fun: fn(e) -> f,
) -> Result(a, f) {
case result {
Ok(x) -> Ok(x)
Error(error) -> Error(fun(error))
}
}
/// Merges a nested `Result` into a single layer.
///
/// ## Examples
///
/// ```gleam
/// > flatten(Ok(Ok(1)))
/// Ok(1)
/// ```
///
/// ```gleam
/// > flatten(Ok(Error("")))
/// Error("")
/// ```
///
/// ```gleam
/// > flatten(Error(Nil))
/// Error(Nil)
/// ```
///
pub fn flatten(result: Result(Result(a, e), e)) -> Result(a, e) {
case result {
Ok(x) -> x
Error(error) -> Error(error)
}
}
/// "Updates" an `Ok` result by passing its value to a function that yields a result,
/// and returning the yielded result. (This may "replace" the `Ok` with an `Error`.)
///
/// If the input is an `Error` rather than an `Ok`, the function is not called and
/// the original `Error` is returned.
///
/// This function is the equivalent of calling `map` followed by `flatten`, and
/// it is useful for chaining together multiple functions that may fail.
///
/// ## Examples
///
/// ```gleam
/// > try(Ok(1), fn(x) { Ok(x + 1) })
/// Ok(2)
/// ```
///
/// ```gleam
/// > try(Ok(1), fn(x) { Ok(#("a", x)) })
/// Ok(#("a", 1))
/// ```
///
/// ```gleam
/// > try(Ok(1), fn(_) { Error("Oh no") })
/// Error("Oh no")
/// ```
///
/// ```gleam
/// > try(Error(Nil), fn(x) { Ok(x + 1) })
/// Error(Nil)
/// ```
///
pub fn try(
result: Result(a, e),
apply fun: fn(a) -> Result(b, e),
) -> Result(b, e) {
case result {
Ok(x) -> fun(x)
Error(e) -> Error(e)
}
}
/// An alias for `try`. See the documentation for that function for more information.
///
pub fn then(
result: Result(a, e),
apply fun: fn(a) -> Result(b, e),
) -> Result(b, e) {
try(result, fun)
}
/// Extracts the `Ok` value from a result, returning a default value if the result
/// is an `Error`.
///
/// ## Examples
///
/// ```gleam
/// > unwrap(Ok(1), 0)
/// 1
/// ```
///
/// ```gleam
/// > unwrap(Error(""), 0)
/// 0
/// ```
///
pub fn unwrap(result: Result(a, e), or default: a) -> a {
case result {
Ok(v) -> v
Error(_) -> default
}
}
/// Extracts the `Ok` value from a result, evaluating the default function if the result
/// is an `Error`.
///
/// ## Examples
///
/// ```gleam
/// > lazy_unwrap(Ok(1), fn() { 0 })
/// 1
/// ```
///
/// ```gleam
/// > lazy_unwrap(Error(""), fn() { 0 })
/// 0
/// ```
///
pub fn lazy_unwrap(result: Result(a, e), or default: fn() -> a) -> a {
case result {
Ok(v) -> v
Error(_) -> default()
}
}
/// Extracts the `Error` value from a result, returning a default value if the result
/// is an `Ok`.
///
/// ## Examples
///
/// ```gleam
/// > unwrap_error(Error(1), 0)
/// 1
/// ```
///
/// ```gleam
/// > unwrap_error(Ok(""), 0)
/// 0
/// ```
///
pub fn unwrap_error(result: Result(a, e), or default: e) -> e {
case result {
Ok(_) -> default
Error(e) -> e
}
}
/// Extracts the inner value from a result. Both the value and error must be of
/// the same type.
///
/// ## Examples
///
/// ```gleam
/// > unwrap_both(Error(1))
/// 1
/// ```
///
/// ```gleam
/// > unwrap_both(Ok(2))
/// 2
/// ```
///
pub fn unwrap_both(result: Result(a, a)) -> a {
case result {
Ok(a) -> a
Error(a) -> a
}
}
/// Transforms any error into `Error(Nil)`.
///
/// ## Examples
///
/// ```gleam
/// > nil_error(Error(1))
/// Error(Nil)
/// ```
///
/// ```gleam
/// > nil_error(Ok(1))
/// Ok(1)
/// ```
///
pub fn nil_error(result: Result(a, e)) -> Result(a, Nil) {
map_error(result, fn(_) { Nil })
}
/// Returns the first value if it is `Ok`, otherwise returns the second value.
///
/// ## Examples
///
/// ```gleam
/// > or(Ok(1), Ok(2))
/// Ok(1)
/// ```
///
/// ```gleam
/// > or(Ok(1), Error("Error 2"))
/// Ok(1)
/// ```
///
/// ```gleam
/// > or(Error("Error 1"), Ok(2))
/// Ok(2)
/// ```
///
/// ```gleam
/// > or(Error("Error 1"), Error("Error 2"))
/// Error("Error 2")
/// ```
///
pub fn or(first: Result(a, e), second: Result(a, e)) -> Result(a, e) {
case first {
Ok(_) -> first
Error(_) -> second
}
}
/// Returns the first value if it is `Ok`, otherwise evaluates the given function for a fallback value.
///
/// ## Examples
///
/// ```gleam
/// > lazy_or(Ok(1), fn() { Ok(2) })
/// Ok(1)
/// ```
///
/// ```gleam
/// > lazy_or(Ok(1), fn() { Error("Error 2") })
/// Ok(1)
/// ```
///
/// ```gleam
/// > lazy_or(Error("Error 1"), fn() { Ok(2) })
/// Ok(2)
/// ```
///
/// ```gleam
/// > lazy_or(Error("Error 1"), fn() { Error("Error 2") })
/// Error("Error 2")
/// ```
///
pub fn lazy_or(
first: Result(a, e),
second: fn() -> Result(a, e),
) -> Result(a, e) {
case first {
Ok(_) -> first
Error(_) -> second()
}
}
/// Combines a list of results into a single result.
/// If all elements in the list are `Ok` then returns an `Ok` holding the list of values.
/// If any element is `Error` then returns the first error.
///
/// ## Examples
///
/// ```gleam
/// > all([Ok(1), Ok(2)])
/// Ok([1, 2])
/// ```
///
/// ```gleam
/// > all([Ok(1), Error("e")])
/// Error("e")
/// ```
///
pub fn all(results: List(Result(a, e))) -> Result(List(a), e) {
list.try_map(results, fn(x) { x })
}
/// Given a list of results, returns a pair where the first element is a list
/// of all the values inside `Ok` and the second element is a list with all the
/// values inside `Error`. The values in both lists appear in reverse order with
/// respect to their position in the original list of results.
///
/// ## Examples
///
/// ```gleam
/// > partition([Ok(1), Error("a"), Error("b"), Ok(2)])
/// #([2, 1], ["b", "a"])
/// ```
///
pub fn partition(results: List(Result(a, e))) -> #(List(a), List(e)) {
do_partition(results, [], [])
}
fn do_partition(results: List(Result(a, e)), oks: List(a), errors: List(e)) {
case results {
[] -> #(oks, errors)
[Ok(a), ..rest] -> do_partition(rest, [a, ..oks], errors)
[Error(e), ..rest] -> do_partition(rest, oks, [e, ..errors])
}
}
/// Replace the value within a result
///
/// ## Examples
///
/// ```gleam
/// > replace(Ok(1), Nil)
/// Ok(Nil)
/// ```
///
/// ```gleam
/// > replace(Error(1), Nil)
/// Error(1)
/// ```
///
pub fn replace(result: Result(a, e), value: b) -> Result(b, e) {
case result {
Ok(_) -> Ok(value)
Error(error) -> Error(error)
}
}
/// Replace the error within a result
///
/// ## Examples
///
/// ```gleam
/// > replace_error(Error(1), Nil)
/// Error(Nil)
/// ```
///
/// ```gleam
/// > replace_error(Ok(1), Nil)
/// Ok(1)
/// ```
///
pub fn replace_error(result: Result(a, e1), error: e2) -> Result(a, e2) {
case result {
Ok(x) -> Ok(x)
Error(_) -> Error(error)
}
}
/// Given a list of results, returns only the values inside `Ok`.
///
/// ## Examples
///
/// ```gleam
/// > values([Ok(1), Error("a"), Ok(3)])
/// [1, 3]
/// ```
///
pub fn values(results: List(Result(a, e))) -> List(a) {
list.filter_map(results, fn(r) { r })
}
/// Updates a value held within the `Error` of a result by calling a given function
/// on it, where the given function also returns a result. The two results are
/// then merged together into one result.
///
/// If the result is an `Ok` rather than `Error` the function is not called and the
/// result stays the same.
///
/// This function is useful for chaining together computations that may fail
/// and trying to recover from possible errors.
///
/// ## Examples
///
/// ```gleam
/// > Ok(1) |> try_recover(with: fn(_) { Error("failed to recover") })
/// Ok(1)
/// ```
///
/// ```gleam
/// > Error(1) |> try_recover(with: fn(error) { Ok(error + 1) })
/// Ok(2)
/// ```
///
/// ```gleam
/// > Error(1) |> try_recover(with: fn(error) { Error("failed to recover") })
/// Error("failed to recover")
/// ```
///
pub fn try_recover(
result: Result(a, e),
with fun: fn(e) -> Result(a, f),
) -> Result(a, f) {
case result {
Ok(value) -> Ok(value)
Error(error) -> fun(error)
}
}