Skip to content

Commit 69b9080

Browse files
Add belt_Option.apply
Signed-Off-By: Florian Hammerschmidt <florianh89@gmail.com>
1 parent ad092f5 commit 69b9080

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

jscomp/others/belt_Option.ml

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
let forEachU opt f = match opt with
26+
| Some x -> (f x [@bs])
27+
| None -> ()
28+
29+
let forEach opt f = forEachU opt (fun[@bs] x -> f x)
2530

2631
let getExn = function
2732
| Some x -> x

jscomp/others/belt_Option.mli

+15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
Utilities for option data type
2828
*)
2929

30+
val forEachU : 'a option -> ('a -> unit [@bs]) -> unit
31+
(** Uncurried version of [forEach] *)
32+
33+
val forEach : 'a option -> ('a -> unit) -> unit
34+
(**
35+
[forEach optionValue f]
36+
37+
If [optionValue] is [Some value], it calls [f value]; otherwise returns [()]
38+
39+
@example {[
40+
forEach (Some "thing")(fun x -> Js.log x);; (* logs "thing" *)
41+
forEach None (fun x -> Js.log x);; (* returns () *)
42+
]}
43+
*)
44+
3045
val getExn : 'a option -> 'a
3146
(** [getExn optionalValue]
3247
Returns [value] if [optionalValue] is [Some value], otherwise raises [getExn]

lib/js/belt_Option.js

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
var Curry = require("./curry.js");
44
var Caml_option = require("./caml_option.js");
55

6+
function forEachU(opt, f) {
7+
if (opt !== undefined) {
8+
return f(Caml_option.valFromOption(opt));
9+
} else {
10+
return /* () */0;
11+
}
12+
}
13+
14+
function forEach(opt, f) {
15+
return forEachU(opt, Curry.__1(f));
16+
}
17+
618
function getExn(param) {
719
if (param !== undefined) {
820
return Caml_option.valFromOption(param);
@@ -95,6 +107,8 @@ function cmp(a, b, f) {
95107
return cmpU(a, b, Curry.__2(f));
96108
}
97109

110+
exports.forEachU = forEachU;
111+
exports.forEach = forEach;
98112
exports.getExn = getExn;
99113
exports.mapWithDefaultU = mapWithDefaultU;
100114
exports.mapWithDefault = mapWithDefault;

0 commit comments

Comments
 (0)