Skip to content

Commit e6ecf93

Browse files
committed
Add option module to belt
1 parent 38b7386 commit e6ecf93

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

Diff for: jscomp/others/Makefile

+1
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\

Diff for: jscomp/others/belt.ml

+4
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ module HashSet = Belt_HashSet
232232
*)
233233
module HashMap = Belt_HashMap
234234

235+
(** {!Belt.Option}
235236
237+
Utilities for option data type
238+
*)
236239

240+
module Option = Belt_Option
237241

238242

Diff for: jscomp/others/belt_Option.ml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
let getExn = function
2+
| Some x -> x
3+
| None -> assert false
4+
5+
let fold opt default f = match opt with
6+
| Some x -> f x
7+
| None -> default
8+
9+
let map opt f = match opt with
10+
| Some x -> Some (f x)
11+
| None -> None
12+
13+
let flatMap opt f = match opt with
14+
| Some x -> f x
15+
| None -> None
16+
17+
let getOrElse opt default = match opt with
18+
| Some x -> x
19+
| None -> default
20+
21+
let exists = function
22+
| Some _ -> true
23+
| None -> false
24+
25+
let empty = function
26+
| Some _ -> false
27+
| None -> true

Diff for: jscomp/others/belt_Option.mli

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
val getExn : 'a option -> 'a
2+
val fold : 'a option -> 'b -> ('a -> 'b) -> 'b
3+
val map : 'a option -> ('a -> 'b) -> 'b option
4+
val flatMap : 'a option -> ('a -> 'b option) -> 'b option
5+
val getOrElse : 'a option -> 'a -> 'a
6+
val exists : 'a option -> bool
7+
val empty : 'a option -> bool

Diff for: lib/js/belt.js

+3
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 */

Diff for: lib/js/belt_Option.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
return /* assert false */0;
10+
}
11+
}
12+
13+
function fold(opt, $$default, f) {
14+
if (opt) {
15+
return Curry._1(f, opt[0]);
16+
} else {
17+
return $$default;
18+
}
19+
}
20+
21+
function map(opt, f) {
22+
if (opt) {
23+
return /* Some */[Curry._1(f, opt[0])];
24+
} else {
25+
return /* None */0;
26+
}
27+
}
28+
29+
function flatMap(opt, f) {
30+
if (opt) {
31+
return Curry._1(f, opt[0]);
32+
} else {
33+
return /* None */0;
34+
}
35+
}
36+
37+
function getOrElse(opt, $$default) {
38+
if (opt) {
39+
return opt[0];
40+
} else {
41+
return $$default;
42+
}
43+
}
44+
45+
function exists(param) {
46+
if (param) {
47+
return /* true */1;
48+
} else {
49+
return /* false */0;
50+
}
51+
}
52+
53+
function empty(param) {
54+
if (param) {
55+
return /* false */0;
56+
} else {
57+
return /* true */1;
58+
}
59+
}
60+
61+
exports.getExn = getExn;
62+
exports.fold = fold;
63+
exports.map = map;
64+
exports.flatMap = flatMap;
65+
exports.getOrElse = getOrElse;
66+
exports.exists = exists;
67+
exports.empty = empty;
68+
/* No side effect */

0 commit comments

Comments
 (0)