forked from ye-man/fnm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResult.re
49 lines (41 loc) · 835 Bytes
/
Result.re
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
let return = x => Ok(x);
let both = (a, b) =>
switch (a, b) {
| (Error(_) as e, _)
| (_, Error(_) as e) => e
| (Ok(ax), Ok(bx)) => Ok((ax, bx))
};
let mapError = (fn, res) =>
switch (res) {
| Error(x) => Error(fn(x))
| Ok(_) as x => x
};
let map = (fn, res) =>
switch (res) {
| Ok(x) => Ok(fn(x))
| Error(_) as e => e
};
let bind = (fn, res) =>
switch (res) {
| Ok(x) => fn(x)
| Error(_) as e => e
};
let fold = (error, ok, res) =>
switch (res) {
| Ok(x) => ok(x)
| Error(x) => error(x)
};
module Let_syntax = {
let map = (x, ~f) => map(f, x);
let bind = (x, ~f) => bind(f, x);
};
let toLwt = res =>
switch (res) {
| Error(x) => Lwt.fail_with(x)
| Ok(x) => Lwt.return(x)
};
let toLwtErr = res =>
switch (res) {
| Error(x) => Lwt.fail(x)
| Ok(x) => Lwt.return(x)
};