Skip to content

Commit d8f50d2

Browse files
authored
Merge pull request rescript-lang#2621 from mchaver/master
Add Result module to Belt
2 parents 6619da4 + 0033bf6 commit d8f50d2

9 files changed

+265
-2
lines changed

jscomp/others/.depend

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ belt_MapString.cmj : belt_internalMapString.cmj belt_internalAVLtree.cmj \
5858
belt_MapInt.cmj : belt_internalMapInt.cmj belt_internalAVLtree.cmj \
5959
belt_Array.cmj belt_MapInt.cmi
6060
belt_Option.cmj : belt_Option.cmi
61+
belt_Result.cmj : belt_Result.cmi
6162
belt_Set.cmj : belt_SetString.cmj belt_SetInt.cmj belt_SetDict.cmj \
6263
belt_Id.cmj belt_Array.cmj belt_Set.cmi
6364
belt_MutableSet.cmj : belt_internalAVLset.cmj belt_SortArray.cmj \
@@ -130,6 +131,7 @@ belt_Map.cmi : belt_MapString.cmi belt_MapInt.cmi belt_MapDict.cmi \
130131
belt_MapString.cmi :
131132
belt_MapInt.cmi :
132133
belt_Option.cmi :
134+
belt_Result.cmi :
133135
belt_Set.cmi : belt_SetString.cmi belt_SetInt.cmi belt_SetDict.cmi \
134136
belt_Id.cmi
135137
belt_MutableSet.cmi : belt_MutableSetString.cmi belt_MutableSetInt.cmi \

jscomp/others/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string
3535
belt_MapString \
3636
belt_MapInt\
3737
belt_Option\
38+
belt_Result\
3839
belt_internalSet\
3940
belt_Set\
4041
belt_MutableSet\

jscomp/others/belt.ml

+9-2
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,20 @@ module HashMap = Belt_HashMap
239239

240240
(** {!Belt.Option}
241241
242-
Utilities for option data type
242+
Utilities for option data type.
243243
*)
244244
module Option = Belt_Option
245245

246246

247+
(** {!Belt.Result}
248+
249+
Utilities for result data type.
250+
*)
251+
252+
module Result = Belt_Result
253+
247254
(** {!Belt.Debug}
248255
249256
Utilities for set up debugging
250257
*)
251-
module Debug = Belt_Debug
258+
module Debug = Belt_Debug

jscomp/others/belt_Result.ml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
(* Copyright (C) 2017 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
type ('a, 'b) t =
26+
| Ok of 'a
27+
| Error of 'b
28+
29+
let getExn = function
30+
| Ok x -> x
31+
| Error _ -> [%assert "getExn"]
32+
33+
let mapWithDefaultU opt default f = match opt with
34+
| Ok x -> (f x [@bs])
35+
| Error _ -> default
36+
37+
let mapWithDefault opt default f = mapWithDefaultU opt default (fun[@bs] x -> f x)
38+
39+
let mapU opt f = match opt with
40+
| Ok x -> Ok (f x [@bs])
41+
| Error y -> Error y
42+
43+
let map opt f = mapU opt (fun[@bs] x -> f x)
44+
45+
let flatMapU opt f = match opt with
46+
| Ok x -> (f x [@bs])
47+
| Error y -> Error y
48+
49+
let flatMap opt f = flatMapU opt (fun[@bs] x -> f x)
50+
51+
let getWithDefault opt default = match opt with
52+
| Ok x -> x
53+
| Error _ -> default
54+
55+
let isOk = function
56+
| Ok _ -> true
57+
| Error _ -> false
58+
59+
let isError = function
60+
| Ok _ -> false
61+
| Error _ -> true
62+
63+
let eqU a b f = match (a, b) with
64+
| (Ok a, Ok b) -> f a b [@bs]
65+
| (Error _, Ok _)
66+
| (Ok _, Error _) -> false
67+
| (Error _, Error _) -> true
68+
69+
let eq a b f = eqU a b (fun[@bs] x y -> f x y)
70+
71+
let cmpU a b f = match (a, b) with
72+
| (Ok a, Ok b) -> f a b [@bs]
73+
| (Error _, Ok _) -> -1
74+
| (Ok _, Error _) -> 1
75+
| (Error _, Error _) -> 0
76+
77+
let cmp a b f = cmpU a b (fun[@bs] x y -> f x y)

jscomp/others/belt_Result.mli

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(* Copyright (C) 2017 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
(** {!Belt.Result}
26+
27+
Utilities for result data type.
28+
*)
29+
30+
type ('a, 'b) t =
31+
| Ok of 'a
32+
| Error of 'b
33+
34+
val getExn : ('a, 'b) t -> 'a
35+
val mapWithDefaultU : ('a, 'c) t -> 'b -> ('a -> 'b [@bs]) -> 'b
36+
val mapWithDefault : ('a, 'c) t -> 'b -> ('a -> 'b) -> 'b
37+
val mapU : ('a, 'c) t -> ('a -> 'b [@bs]) -> ('b, 'c) t
38+
val map : ('a, 'c) t -> ('a -> 'b) -> ('b, 'c) t
39+
val flatMapU : ('a, 'c) t -> ('a -> ('b, 'c) t [@bs]) -> ('b, 'c) t
40+
val flatMap : ('a, 'c) t -> ('a -> ('b, 'c) t) -> ('b, 'c) t
41+
val getWithDefault : ('a, 'b) t -> 'a -> 'a
42+
val isOk : ('a, 'b) t -> bool
43+
val isError : ('a, 'b) t -> bool
44+
val eqU : ('a, 'c) t -> ('b, 'd) t -> ('a -> 'b -> bool [@bs]) -> bool
45+
val eq : ('a, 'c) t -> ('b, 'd) t -> ('a -> 'b -> bool) -> bool
46+
val cmpU : ('a, 'c) t -> ('b, 'd) t -> ('a -> 'b -> int [@bs]) -> int
47+
val cmp : ('a, 'c) t -> ('b, 'd) t -> ('a -> 'b -> int) -> int

jscomp/others/js_result.ml

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
type (+'good, +'bad) t =
2626
| Ok of 'good
2727
| Error of 'bad
28+
[@@ocaml.deprecated "Please use `Belt.Result.t` instead"]

jscomp/others/js_result.mli

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
type (+'good, +'bad) t =
2626
| Ok of 'good
2727
| Error of 'bad
28+
[@@ocaml.deprecated "Please use `Belt.Result.t` instead"]

lib/js/belt.js

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ var HashMap = 0;
2929

3030
var Option = 0;
3131

32+
var Result = 0;
33+
3234
var Debug = 0;
3335

3436
exports.Id = Id;
@@ -45,5 +47,6 @@ exports.MutableMap = MutableMap;
4547
exports.HashSet = HashSet;
4648
exports.HashMap = HashMap;
4749
exports.Option = Option;
50+
exports.Result = Result;
4851
exports.Debug = Debug;
4952
/* No side effect */

lib/js/belt_Result.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
'use strict';
2+
3+
var Block = require("./block.js");
4+
var Curry = require("./curry.js");
5+
6+
function getExn(param) {
7+
if (param.tag) {
8+
throw new Error("getExn");
9+
} else {
10+
return param[0];
11+
}
12+
}
13+
14+
function mapWithDefaultU(opt, $$default, f) {
15+
if (opt.tag) {
16+
return $$default;
17+
} else {
18+
return f(opt[0]);
19+
}
20+
}
21+
22+
function mapWithDefault(opt, $$default, f) {
23+
return mapWithDefaultU(opt, $$default, Curry.__1(f));
24+
}
25+
26+
function mapU(opt, f) {
27+
if (opt.tag) {
28+
return /* Error */Block.__(1, [opt[0]]);
29+
} else {
30+
return /* Ok */Block.__(0, [f(opt[0])]);
31+
}
32+
}
33+
34+
function map(opt, f) {
35+
return mapU(opt, Curry.__1(f));
36+
}
37+
38+
function flatMapU(opt, f) {
39+
if (opt.tag) {
40+
return /* Error */Block.__(1, [opt[0]]);
41+
} else {
42+
return f(opt[0]);
43+
}
44+
}
45+
46+
function flatMap(opt, f) {
47+
return flatMapU(opt, Curry.__1(f));
48+
}
49+
50+
function getWithDefault(opt, $$default) {
51+
if (opt.tag) {
52+
return $$default;
53+
} else {
54+
return opt[0];
55+
}
56+
}
57+
58+
function isOk(param) {
59+
if (param.tag) {
60+
return /* false */0;
61+
} else {
62+
return /* true */1;
63+
}
64+
}
65+
66+
function isError(param) {
67+
if (param.tag) {
68+
return /* true */1;
69+
} else {
70+
return /* false */0;
71+
}
72+
}
73+
74+
function eqU(a, b, f) {
75+
if (a.tag) {
76+
if (b.tag) {
77+
return /* true */1;
78+
} else {
79+
return /* false */0;
80+
}
81+
} else if (b.tag) {
82+
return /* false */0;
83+
} else {
84+
return f(a[0], b[0]);
85+
}
86+
}
87+
88+
function eq(a, b, f) {
89+
return eqU(a, b, Curry.__2(f));
90+
}
91+
92+
function cmpU(a, b, f) {
93+
if (a.tag) {
94+
if (b.tag) {
95+
return 0;
96+
} else {
97+
return -1;
98+
}
99+
} else if (b.tag) {
100+
return 1;
101+
} else {
102+
return f(a[0], b[0]);
103+
}
104+
}
105+
106+
function cmp(a, b, f) {
107+
return cmpU(a, b, Curry.__2(f));
108+
}
109+
110+
exports.getExn = getExn;
111+
exports.mapWithDefaultU = mapWithDefaultU;
112+
exports.mapWithDefault = mapWithDefault;
113+
exports.mapU = mapU;
114+
exports.map = map;
115+
exports.flatMapU = flatMapU;
116+
exports.flatMap = flatMap;
117+
exports.getWithDefault = getWithDefault;
118+
exports.isOk = isOk;
119+
exports.isError = isError;
120+
exports.eqU = eqU;
121+
exports.eq = eq;
122+
exports.cmpU = cmpU;
123+
exports.cmp = cmp;
124+
/* No side effect */

0 commit comments

Comments
 (0)