Skip to content

Commit 799b561

Browse files
authored
Merge pull request #2622 from arnarthor/master
Add option module to Belt
2 parents faffda5 + 9452c2a commit 799b561

File tree

6 files changed

+249
-1
lines changed

6 files changed

+249
-1
lines changed

jscomp/others/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string
3434
belt_internalMapString\
3535
belt_MapString \
3636
belt_MapInt\
37+
belt_Option\
3738
belt_internalSet\
3839
belt_Set\
3940
belt_MutableSet\

jscomp/others/belt.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ module HashSet = Belt_HashSet
236236
*)
237237
module HashMap = Belt_HashMap
238238

239+
240+
(** {!Belt.Option}
239241
240-
242+
Utilities for option data type
243+
*)
244+
module Option = Belt_Option
241245

242246

jscomp/others/belt_Option.ml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
26+
let getExn = function
27+
| Some x -> x
28+
| None -> [%assert "getExn"]
29+
30+
let mapWithDefaultU opt default f = match opt with
31+
| Some x -> (f x [@bs])
32+
| None -> default
33+
34+
let mapWithDefault opt default f = mapWithDefaultU opt default (fun[@bs] x -> f x)
35+
36+
let mapU opt f = match opt with
37+
| Some x -> Some (f x [@bs])
38+
| None -> None
39+
40+
let map opt f = mapU opt (fun[@bs] x -> f x)
41+
42+
let flatMapU opt f = match opt with
43+
| Some x -> (f x [@bs])
44+
| None -> None
45+
46+
let flatMap opt f = flatMapU opt (fun[@bs] x -> f x)
47+
48+
let getWithDefault opt default = match opt with
49+
| Some x -> x
50+
| None -> default
51+
52+
let isSome = function
53+
| Some _ -> true
54+
| None -> false
55+
56+
let isNone = function
57+
| Some _ -> false
58+
| None -> true
59+
60+
let eqU a b f = match (a, b) with
61+
| (Some a, Some b) -> f a b [@bs]
62+
| (None, Some _)
63+
| (Some _, None) -> false
64+
| (None, None) -> true
65+
66+
let eq a b f = eqU a b (fun[@bs] x y -> f x y)
67+
68+
let cmpU a b f = match (a, b) with
69+
| (Some a, Some b) -> f a b [@bs]
70+
| (None, Some _) -> -1
71+
| (Some _, None) -> 1
72+
| (None, None) -> 0
73+
74+
let cmp a b f = cmpU a b (fun[@bs] x y -> f x y)

jscomp/others/belt_Option.mli

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.Option}
26+
27+
Utilities for option data type
28+
*)
29+
30+
val getExn : 'a option -> 'a
31+
val mapWithDefaultU : 'a option -> 'b -> ('a -> 'b [@bs]) -> 'b
32+
val mapWithDefault : 'a option -> 'b -> ('a -> 'b) -> 'b
33+
val mapU : 'a option -> ('a -> 'b [@bs]) -> 'b option
34+
val map : 'a option -> ('a -> 'b) -> 'b option
35+
val flatMapU : 'a option -> ('a -> 'b option [@bs]) -> 'b option
36+
val flatMap : 'a option -> ('a -> 'b option) -> 'b option
37+
val getWithDefault : 'a option -> 'a -> 'a
38+
val isSome : 'a option -> bool
39+
val isNone : 'a option -> bool
40+
val eqU : 'a option -> 'b option -> ('a -> 'b -> bool [@bs]) -> bool
41+
val eq : 'a option -> 'b option -> ('a -> 'b -> bool) -> bool
42+
val cmpU : 'a option -> 'b option -> ('a -> 'b -> int [@bs]) -> int
43+
val cmp : 'a option -> 'b option -> ('a -> 'b -> int) -> int

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 Option = 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.Option = Option;
4346
/* No side effect */

lib/js/belt_Option.js

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

0 commit comments

Comments
 (0)