Skip to content

Commit 130db55

Browse files
committed
Move to result
1 parent 3f52573 commit 130db55

File tree

6 files changed

+94
-130
lines changed

6 files changed

+94
-130
lines changed

jscomp/others/belt.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ module HashSet = Belt_HashSet
232232
*)
233233
module HashMap = Belt_HashMap
234234

235-
(** {!Belt.Either}
235+
(** {!Belt.Result}
236236
237237
The top level provides generic {b immutable} either operations.
238238
*)
239239

240-
module Either = Belt_Either
240+
module Result = Belt_Result

jscomp/others/belt_Either.ml

Lines changed: 0 additions & 87 deletions
This file was deleted.

jscomp/others/belt_Either.mli

Lines changed: 0 additions & 41 deletions
This file was deleted.

jscomp/others/belt_Result.ml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+

jscomp/others/belt_Result.mli

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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

lib/js/belt.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var HashSet = 0;
2727

2828
var HashMap = 0;
2929

30+
var Either = 0;
31+
3032
exports.Id = Id;
3133
exports.$$Array = $$Array;
3234
exports.SortArray = SortArray;
@@ -40,4 +42,5 @@ exports.MutableSet = MutableSet;
4042
exports.MutableMap = MutableMap;
4143
exports.HashSet = HashSet;
4244
exports.HashMap = HashMap;
45+
exports.Either = Either;
4346
/* No side effect */

0 commit comments

Comments
 (0)