Skip to content

Commit 28bf74e

Browse files
committed
feat(nullable): add mapWithDefault
1 parent 061a9c6 commit 28bf74e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Core__Nullable.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,19 @@ function map(value, f) {
3636

3737
}
3838

39+
function mapWithDefault(value, $$default, f) {
40+
if (value == null) {
41+
return $$default;
42+
} else {
43+
return Curry._1(f, value);
44+
}
45+
}
46+
3947
export {
4048
fromOption ,
4149
getWithDefault ,
4250
getExn ,
4351
map ,
52+
mapWithDefault ,
4453
}
4554
/* No side effect */

src/Core__Nullable.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ let map = (value, f) =>
3333
| Some(x) => Some(f(x))
3434
| None => None
3535
}
36+
37+
let mapWithDefault = (value, default, f) =>
38+
switch value->toOption {
39+
| Some(x) => f(x)
40+
| None => default
41+
}

0 commit comments

Comments
 (0)