Skip to content

Commit 0dcadc7

Browse files
committed
Option.exists and Option.isNoneOr
1 parent 49537f2 commit 0dcadc7

File tree

7 files changed

+182
-3
lines changed

7 files changed

+182
-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 both `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. This function is similar to `Array.some` and acts like the "there exists" quantifier in mathematics. In particular it returns false for an "empty" option with a value of `None`.
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 either `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. This function is similar to `Array.every` and acts like the "for all" quantifier in mathematics. In particular it returns true for an "empty" option with a value of `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+
13,
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+
19,
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+
25,
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+
27,
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+
33,
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
Test.run(
13+
__POS_OF__("exists: if Some and true, return true"),
14+
Some(1)->Option.exists(isPositive),
15+
eq,
16+
true,
17+
)
18+
Test.run(
19+
__POS_OF__("exists: if Some and false, return false"),
20+
Some(-1)->Option.exists(isPositive),
21+
eq,
22+
false,
23+
)
24+
25+
Test.run(__POS_OF__("isNoneOr: if None, return true"), None->Option.isNoneOr(isPositive), eq, true)
26+
Test.run(
27+
__POS_OF__("isNoneOr: if Some and true, return true"),
28+
Some(1)->Option.isNoneOr(isPositive),
29+
eq,
30+
true,
31+
)
32+
Test.run(
33+
__POS_OF__("isNoneOr: if Some and false, return false"),
34+
Some(-1)->Option.isNoneOr(isPositive),
35+
eq,
36+
false,
37+
)

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

910
var bign = TestTests.bign;
@@ -26,10 +27,12 @@ var Concurrently = PromiseTest.Concurrently;
2627

2728
var panicTest = ErrorTests.panicTest;
2829

29-
var eq = IntTests.eq;
30-
3130
var $$catch = IntTests.$$catch;
3231

32+
var eq = OptionTests.eq;
33+
34+
var isPositive = OptionTests.isPositive;
35+
3336
export {
3437
bign ,
3538
TestError ,
@@ -41,7 +44,8 @@ export {
4144
Catching ,
4245
Concurrently ,
4346
panicTest ,
44-
eq ,
4547
$$catch ,
48+
eq ,
49+
isPositive ,
4650
}
4751
/* IntTests Not a pure module */

test/TestSuite.res

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

0 commit comments

Comments
 (0)