Skip to content

Commit 139ecfb

Browse files
committed
feat(option): add flat
1 parent 847e916 commit 139ecfb

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
@@ -3,6 +3,13 @@
33
import * as Curry from "rescript/lib/es6/curry.js";
44
import * as Caml_option from "rescript/lib/es6/caml_option.js";
55

6+
function flat(opt) {
7+
if (opt !== undefined) {
8+
return Caml_option.valFromOption(opt);
9+
}
10+
11+
}
12+
613
function filter(opt, p) {
714
var p$1 = Curry.__1(p);
815
if (opt !== undefined && p$1(Caml_option.valFromOption(opt))) {
@@ -107,6 +114,7 @@ function cmp(a, b, f) {
107114
}
108115

109116
export {
117+
flat ,
110118
filter ,
111119
forEach ,
112120
getExn ,

src/Core__Option.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
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 flat = opt =>
26+
switch opt {
27+
| Some(v) => v
28+
| None => None
29+
}
30+
2531
let filterU = (opt, p) =>
2632
switch opt {
2733
| 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
@@ -39,6 +39,18 @@ let someString: option<string> = Some("hello")
3939
```
4040
*/
4141

42+
/**
43+
`flat(value)` flattens a nested `option` value to a single level..
44+
45+
## Examples
46+
47+
```rescript
48+
Option.float(Some(Some(10))) // Some(10)
49+
Option.flat(Some(None)) // None
50+
```
51+
*/
52+
let flat: option<option<'a>> => option<'a>
53+
4254
/**
4355
`filter(opt, f)` applies `f` to `opt`, if `f` returns `true`, then it returns `Some(value)`, otherwise returns `None`.
4456

0 commit comments

Comments
 (0)