Skip to content

Commit 7d171cc

Browse files
committed
Merge branch
2 parents 3b84757 + fb0e9f4 commit 7d171cc

File tree

7 files changed

+186
-3
lines changed

7 files changed

+186
-3
lines changed

src/Core__Option.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ function cmp(a, b, f) {
106106
}
107107
}
108108

109+
function exists(o, p) {
110+
if (o !== undefined) {
111+
return Curry._1(p, Caml_option.valFromOption(o));
112+
} else {
113+
return false;
114+
}
115+
}
116+
117+
function isNoneOr(o, p) {
118+
if (o !== undefined) {
119+
return Curry._1(p, Caml_option.valFromOption(o));
120+
} else {
121+
return true;
122+
}
123+
}
124+
109125
export {
110126
filter ,
111127
forEach ,
@@ -119,5 +135,7 @@ export {
119135
isNone ,
120136
eq ,
121137
cmp ,
138+
exists ,
139+
isNoneOr ,
122140
}
123141
/* No side effect */

src/Core__Option.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,15 @@ let cmpU = (a, b, f) =>
111111
}
112112

113113
let cmp = (a, b, f) => cmpU(a, b, (. x, y) => f(x, y))
114+
115+
let exists = (o, p) =>
116+
switch o {
117+
| None => false
118+
| Some(v) => p(v)
119+
}
120+
121+
let isNoneOr = (o, p) =>
122+
switch o {
123+
| None => true
124+
| Some(v) => p(v)
125+
}

src/Core__Option.resi

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,33 @@ cmp(None, None, clockCompare) // 0
248248
```
249249
*/
250250
let cmp: (option<'a>, option<'b>, ('a, 'b) => int) => int
251+
252+
/**
253+
`exists(option, predicate)` tests whether the option is `Some` **and** the predicate applied to its value is true.
254+
255+
An option can be thought of as an array with 0 or 1 items in it. `exists` is similar to `Array.some` and acts like the "there exists" quantifier in mathematics. It returns false for a `None` option.
256+
257+
## Examples
258+
259+
```rescript
260+
Option.exists(None, i => i >= 0) // false
261+
Option.exists(Some(3), i => i > 1) // true
262+
Option.exists(Some(3), i => i < 0) // false
263+
```
264+
*/
265+
let exists: (option<'a>, 'a => bool) => bool
266+
267+
/**
268+
`isNoneOr(option, predicate)` tests whether the option is `None` **or** the predicate applied to its value is true.
269+
270+
An option can be thought of as an array with 0 or 1 items in it. `isNoneOr` is similar to `Array.every` and acts like the "for all" quantifier in mathematics. In particular it returns true when the option is `None`.
271+
272+
## Examples
273+
274+
```rescript
275+
Option.isNoneOr(None, i => i >= 0) // true
276+
Option.isNoneOr(Some(3), i => i > 1) // true
277+
Option.isNoneOr(Some(3), i => i < 0) // false
278+
```
279+
*/
280+
let isNoneOr: (option<'a>, 'a => bool) => bool

test/OptionTests.mjs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
5+
import * as Core__Option from "../src/Core__Option.mjs";
6+
7+
var eq = Caml_obj.equal;
8+
9+
function isPositive(i) {
10+
return i > 0;
11+
}
12+
13+
Test.run([
14+
[
15+
"OptionTests.res",
16+
11,
17+
20,
18+
51
19+
],
20+
"exists: if None, return false"
21+
], Core__Option.exists(undefined, isPositive), eq, false);
22+
23+
Test.run([
24+
[
25+
"OptionTests.res",
26+
14,
27+
13,
28+
52
29+
],
30+
"exists: if Some and true, return true"
31+
], Core__Option.exists(1, isPositive), eq, true);
32+
33+
Test.run([
34+
[
35+
"OptionTests.res",
36+
21,
37+
13,
38+
54
39+
],
40+
"exists: if Some and false, return false"
41+
], Core__Option.exists(-1, isPositive), eq, false);
42+
43+
Test.run([
44+
[
45+
"OptionTests.res",
46+
27,
47+
20,
48+
52
49+
],
50+
"isNoneOr: if None, return true"
51+
], Core__Option.isNoneOr(undefined, isPositive), eq, true);
52+
53+
Test.run([
54+
[
55+
"OptionTests.res",
56+
30,
57+
13,
58+
54
59+
],
60+
"isNoneOr: if Some and true, return true"
61+
], Core__Option.isNoneOr(1, isPositive), eq, true);
62+
63+
Test.run([
64+
[
65+
"OptionTests.res",
66+
37,
67+
13,
68+
56
69+
],
70+
"isNoneOr: if Some and false, return false"
71+
], Core__Option.isNoneOr(-1, isPositive), eq, false);
72+
73+
export {
74+
eq ,
75+
isPositive ,
76+
}
77+
/* Not a pure module */

test/OptionTests.res

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
// ===================
6+
// exists and isNoneOr
7+
// ===================
8+
9+
let isPositive = i => i > 0
10+
11+
Test.run(__POS_OF__("exists: if None, return false"), None->Option.exists(isPositive), eq, false)
12+
13+
Test.run(
14+
__POS_OF__("exists: if Some and true, return true"),
15+
Some(1)->Option.exists(isPositive),
16+
eq,
17+
true,
18+
)
19+
20+
Test.run(
21+
__POS_OF__("exists: if Some and false, return false"),
22+
Some(-1)->Option.exists(isPositive),
23+
eq,
24+
false,
25+
)
26+
27+
Test.run(__POS_OF__("isNoneOr: if None, return true"), None->Option.isNoneOr(isPositive), eq, true)
28+
29+
Test.run(
30+
__POS_OF__("isNoneOr: if Some and true, return true"),
31+
Some(1)->Option.isNoneOr(isPositive),
32+
eq,
33+
true,
34+
)
35+
36+
Test.run(
37+
__POS_OF__("isNoneOr: if Some and false, return false"),
38+
Some(-1)->Option.isNoneOr(isPositive),
39+
eq,
40+
false,
41+
)

test/TestSuite.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as IntTests from "./IntTests.mjs";
44
import * as TestTests from "./TestTests.mjs";
55
import * as ArrayTests from "./ArrayTests.mjs";
66
import * as ErrorTests from "./ErrorTests.mjs";
7+
import * as OptionTests from "./OptionTests.mjs";
78
import * as PromiseTest from "./PromiseTest.mjs";
89
import * as ResultTests from "./ResultTests.mjs";
910

@@ -29,12 +30,14 @@ var panicTest = ErrorTests.panicTest;
2930

3031
var $$catch = IntTests.$$catch;
3132

32-
var eq = ResultTests.eq;
33-
3433
var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction;
3534

3635
var forEachIfErrorDoNotCallFunction = ResultTests.forEachIfErrorDoNotCallFunction;
3736

37+
var eq = OptionTests.eq;
38+
39+
var isPositive = OptionTests.isPositive;
40+
3841
export {
3942
bign ,
4043
TestError ,
@@ -47,8 +50,9 @@ export {
4750
Concurrently ,
4851
panicTest ,
4952
$$catch ,
50-
eq ,
5153
forEachIfOkCallFunction ,
5254
forEachIfErrorDoNotCallFunction ,
55+
eq ,
56+
isPositive ,
5357
}
5458
/* IntTests Not a pure module */

test/TestSuite.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ include ErrorTests
44
include ArrayTests
55
include IntTests
66
include ResultTests
7+
include OptionTests

0 commit comments

Comments
 (0)