File tree Expand file tree Collapse file tree 6 files changed +94
-130
lines changed Expand file tree Collapse file tree 6 files changed +94
-130
lines changed Original file line number Diff line number Diff line change @@ -232,9 +232,9 @@ module HashSet = Belt_HashSet
232
232
*)
233
233
module HashMap = Belt_HashMap
234
234
235
- (* * {!Belt.Either }
235
+ (* * {!Belt.Result }
236
236
237
237
The top level provides generic {b immutable} either operations.
238
238
*)
239
239
240
- module Either = Belt_Either
240
+ module Result = Belt_Result
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ (* https://gist.github.com/NicolasT/65dad40b203da7c65b4c *)
2
+
3
+ type ('a, 'b) t =
4
+ | Ok of 'a
5
+ | Error of 'b
6
+
7
+ (* * Constructor functions *)
8
+ let ok a = Ok a
9
+ let error b = error b
10
+
11
+ let result a b = function
12
+ | Ok v -> a v
13
+ | Error v -> b v
14
+
15
+ (* * Bifunctor interface *)
16
+ let bimap a b = result (fun v -> ok (a v)) (fun v -> error (b v))
17
+
18
+ external id : 'a -> 'a = " %identity"
19
+ let const v = fun _ -> v
20
+
21
+ (* * Functor interface *)
22
+ let map f = bimap id f
23
+ let (< $> ) = map
24
+ let mapLeft f = bimap f id
25
+
26
+ (* * Predicates *)
27
+ let isOk v = result (const true ) (const false ) v
28
+ let isError v = result (const false ) (const true ) v
29
+
30
+ let toString a b = result
31
+ (fun v -> " Ok (" ^ (a v) ^ " )" )
32
+ (fun v -> " Error (" ^ (b v) ^ " )" )
33
+
34
+ let fold f z = result (const z) (fun v -> f v z)
35
+
36
+ let oks xs =
37
+ List. fold_left(fun acc x ->
38
+ match x with
39
+ | Ok a -> List. append acc [a]
40
+ | Error _ -> acc
41
+ ) [] xs
42
+
43
+ let errors xs =
44
+ List. fold_left(fun acc x ->
45
+ match x with
46
+ | Ok _ -> acc
47
+ | Error b -> List. append acc [b]
48
+ ) [] xs
49
+
50
+ let arrayLefts xs =
51
+ Array. fold_left(fun acc x ->
52
+ match x with
53
+ | Ok a -> Array. append acc [|a|]
54
+ | Error _ -> acc
55
+ ) [||] xs
56
+
57
+ let arrayRights xs =
58
+ Array. fold_left(fun acc x ->
59
+ match x with
60
+ | Ok _ -> acc
61
+ | Error b -> Array. append acc [|b|]
62
+ ) [||] xs
63
+
Original file line number Diff line number Diff line change
1
+ type ('a, 'b) t =
2
+ | Ok of 'a
3
+ | Error of 'b
4
+
5
+ val ok : 'a -> ('a , 'b ) t
6
+ val error : 'b -> ('a , 'b ) t
7
+
8
+ val result : ('a -> 'c ) -> ('b -> 'c ) -> ('a , 'b ) t -> 'c
9
+
10
+ val map : ('b -> 'c ) -> ('a , 'b ) t -> ('a , 'c ) t
11
+ val (< $> ) : ('b -> 'c) -> ('a, 'b) t -> ('a, 'c) t
12
+ val mapLeft : ('a -> 'c ) -> ('a , 'b ) t -> ('c , 'b ) t
13
+
14
+ val bimap : ('a -> 'c ) -> ('b -> 'd ) -> ('a , 'b ) t -> ('c , 'd ) t
15
+
16
+ val isOk : ('a , 'b ) t -> bool
17
+ val isError : ('a , 'b ) t -> bool
18
+
19
+ val toString : ('a -> string ) -> ('b -> string ) -> ('a , 'b ) t -> string
20
+
21
+ val fold : ('b -> 'c -> 'c ) -> 'c -> ('a , 'b ) t -> 'c
22
+
23
+ val oks : (('a , 'b ) t ) list -> 'a list
24
+ val errors : (('a , 'b ) t ) list -> 'b list
25
+ val arrayErrors : (('a , 'b ) t ) array -> 'a array
26
+ val arrayOks : (('a , 'b ) t ) array -> 'b array
Original file line number Diff line number Diff line change @@ -27,6 +27,8 @@ var HashSet = 0;
27
27
28
28
var HashMap = 0 ;
29
29
30
+ var Either = 0 ;
31
+
30
32
exports . Id = Id ;
31
33
exports . $$Array = $$Array ;
32
34
exports . SortArray = SortArray ;
@@ -40,4 +42,5 @@ exports.MutableSet = MutableSet;
40
42
exports . MutableMap = MutableMap ;
41
43
exports . HashSet = HashSet ;
42
44
exports . HashMap = HashMap ;
45
+ exports . Either = Either ;
43
46
/* No side effect */
You can’t perform that action at this time.
0 commit comments