Skip to content

Commit ce95b60

Browse files
committed
feat(option): add flat
1 parent 599d286 commit ce95b60

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/Core__Option.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ function some(value) {
77
return Caml_option.some(value);
88
}
99

10+
function flat(opt) {
11+
if (opt !== undefined) {
12+
return Caml_option.valFromOption(opt);
13+
}
14+
15+
}
16+
1017
function filter(opt, p) {
1118
var p$1 = Curry.__1(p);
1219
if (opt !== undefined && p$1(Caml_option.valFromOption(opt))) {
@@ -112,6 +119,7 @@ function cmp(a, b, f) {
112119

113120
export {
114121
some ,
122+
flat ,
115123
filter ,
116124
forEach ,
117125
getExn ,

src/Core__Option.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
let some = value => Some(value)
2626

27+
let flat = opt =>
28+
switch opt {
29+
| Some(v) => v
30+
| None => None
31+
}
32+
2733
let filterU = (opt, p) =>
2834
switch opt {
2935
| Some(x) as some if p(. x) => some

src/Core__Option.resi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ Option.some("foo") // Some("foo")
5151
*/
5252
let some: 'a => option<'a>
5353

54+
/**
55+
`flat(value)` flattens a nested `option` value to a single level..
56+
57+
## Examples
58+
59+
```rescript
60+
Option.float(Some(Some(10))) // Some(10)
61+
Option.flat(Some(None)) // None
62+
```
63+
*/
64+
let flat: option<option<'a>> => option<'a>
65+
5466
/**
5567
`filter(opt, f)` applies `f` to `opt`, if `f` returns `true`, then it returns `Some(value)`, otherwise returns `None`.
5668

0 commit comments

Comments
 (0)