Skip to content

Commit 653c87c

Browse files
committed
belt_Option add uncurried versions of map/fold/flatMap
1 parent 781ad87 commit 653c87c

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

Diff for: jscomp/others/belt_Option.ml

+12-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@ let getExn = function
22
| Some x -> x
33
| None -> [%assert "getExn"]
44

5-
let fold opt default f = match opt with
6-
| Some x -> f x
5+
let foldU opt default f = match opt with
6+
| Some x -> (f x [@bs])
77
| None -> default
88

9-
let map opt f = match opt with
10-
| Some x -> Some (f x)
9+
let fold opt default f = foldU opt default (fun[@bs] x -> f x)
10+
11+
let mapU opt f = match opt with
12+
| Some x -> Some (f x [@bs])
1113
| None -> None
1214

13-
let flatMap opt f = match opt with
14-
| Some x -> f x
15+
let map opt f = mapU opt (fun[@bs] x -> f x)
16+
17+
let flatMapU opt f = match opt with
18+
| Some x -> (f x [@bs])
1519
| None -> None
1620

21+
let flatMap opt f = flatMapU opt (fun[@bs] x -> f x)
22+
1723
let getOrElse opt default = match opt with
1824
| Some x -> x
1925
| None -> default

Diff for: jscomp/others/belt_Option.mli

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
val getExn : 'a option -> 'a
2+
val foldU : 'a option -> 'b -> ('a -> 'b [@bs]) -> 'b
23
val fold : 'a option -> 'b -> ('a -> 'b) -> 'b
4+
val mapU : 'a option -> ('a -> 'b [@bs]) -> 'b option
35
val map : 'a option -> ('a -> 'b) -> 'b option
6+
val flatMapU : 'a option -> ('a -> 'b option [@bs]) -> 'b option
47
val flatMap : 'a option -> ('a -> 'b option) -> 'b option
58
val getOrElse : 'a option -> 'a -> 'a
69
val has : 'a option -> bool

Diff for: lib/js/belt_Option.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,46 @@ function getExn(param) {
66
if (param) {
77
return param[0];
88
} else {
9-
return /* assert false */0;
9+
throw new Error("getExn");
1010
}
1111
}
1212

13-
function fold(opt, $$default, f) {
13+
function foldU(opt, $$default, f) {
1414
if (opt) {
15-
return Curry._1(f, opt[0]);
15+
return f(opt[0]);
1616
} else {
1717
return $$default;
1818
}
1919
}
2020

21-
function map(opt, f) {
21+
function fold(opt, $$default, f) {
22+
return foldU(opt, $$default, Curry.__1(f));
23+
}
24+
25+
function mapU(opt, f) {
2226
if (opt) {
23-
return /* Some */[Curry._1(f, opt[0])];
27+
return /* Some */[f(opt[0])];
2428
} else {
2529
return /* None */0;
2630
}
2731
}
2832

29-
function flatMap(opt, f) {
33+
function map(opt, f) {
34+
return mapU(opt, Curry.__1(f));
35+
}
36+
37+
function flatMapU(opt, f) {
3038
if (opt) {
31-
return Curry._1(f, opt[0]);
39+
return f(opt[0]);
3240
} else {
3341
return /* None */0;
3442
}
3543
}
3644

45+
function flatMap(opt, f) {
46+
return flatMapU(opt, Curry.__1(f));
47+
}
48+
3749
function getOrElse(opt, $$default) {
3850
if (opt) {
3951
return opt[0];
@@ -42,15 +54,15 @@ function getOrElse(opt, $$default) {
4254
}
4355
}
4456

45-
function exists(param) {
57+
function has(param) {
4658
if (param) {
4759
return /* true */1;
4860
} else {
4961
return /* false */0;
5062
}
5163
}
5264

53-
function empty(param) {
65+
function isEmpty(param) {
5466
if (param) {
5567
return /* false */0;
5668
} else {
@@ -59,10 +71,13 @@ function empty(param) {
5971
}
6072

6173
exports.getExn = getExn;
74+
exports.foldU = foldU;
6275
exports.fold = fold;
76+
exports.mapU = mapU;
6377
exports.map = map;
78+
exports.flatMapU = flatMapU;
6479
exports.flatMap = flatMap;
6580
exports.getOrElse = getOrElse;
66-
exports.exists = exists;
67-
exports.empty = empty;
81+
exports.has = has;
82+
exports.isEmpty = isEmpty;
6883
/* No side effect */

0 commit comments

Comments
 (0)