Skip to content

Commit b03196a

Browse files
committed
Add eq and cmp functions
1 parent fa45dd9 commit b03196a

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

Diff for: jscomp/others/belt_Option.ml

+16
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,19 @@ let isSome = function
5656
let isNone = function
5757
| Some _ -> false
5858
| 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)

Diff for: jscomp/others/belt_Option.mli

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ val flatMap : 'a option -> ('a -> 'b option) -> 'b option
3737
val getWithDefault : 'a option -> 'a -> 'a
3838
val isSome : 'a option -> bool
3939
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

Diff for: lib/js/belt_Option.js

+43-3
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,54 @@ function isNone(param) {
7070
}
7171
}
7272

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+
73109
exports.getExn = getExn;
74110
exports.foldU = foldU;
75111
exports.fold = fold;
76112
exports.mapU = mapU;
77113
exports.map = map;
78114
exports.flatMapU = flatMapU;
79115
exports.flatMap = flatMap;
80-
exports.getOrElse = getOrElse;
81-
exports.has = has;
82-
exports.isEmpty = isEmpty;
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;
83123
/* No side effect */

0 commit comments

Comments
 (0)