Skip to content

Commit 52da4fb

Browse files
tsnobipzth
authored andcommitted
add forEach to Null and Nullable
1 parent 87227b7 commit 52da4fb

6 files changed

+55
-0
lines changed

Diff for: src/Core__Null.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ function getExn(value) {
3939
};
4040
}
4141

42+
function forEach(value, f) {
43+
if (value !== null) {
44+
return Curry._1(f, value);
45+
}
46+
47+
}
48+
4249
function map(value, f) {
4350
if (value !== null) {
4451
return Curry._1(f, value);
@@ -74,6 +81,7 @@ export {
7481
getOr ,
7582
getWithDefault ,
7683
getExn ,
84+
forEach ,
7785
map ,
7886
mapOr ,
7987
mapWithDefault ,

Diff for: src/Core__Null.res

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ let getExn: t<'a> => 'a = value =>
3434

3535
external getUnsafe: t<'a> => 'a = "%identity"
3636

37+
let forEach = (value, f) =>
38+
switch value->toOption {
39+
| Some(x) => f(x)
40+
| None => ()
41+
}
42+
3743
let map = (value, f) =>
3844
switch value->toOption {
3945
| Some(x) => make(f(x))

Diff for: src/Core__Null.resi

+13
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ Null.getUnsafe(null) // Raises an error
128128
*/
129129
external getUnsafe: t<'a> => 'a = "%identity"
130130

131+
/**
132+
`forEach(value, f)` call `f` on `value`. if `value` is not `null`, then if calls
133+
`f`, otherwise returns `unit`.
134+
135+
## Examples
136+
137+
```rescript
138+
Null.forEach(Null.make("thing"), x => Console.log(x)) // logs "thing"
139+
Null.forEach(null, x => Console.log(x)) // returns ()
140+
```
141+
*/
142+
let forEach: (t<'a>, 'a => unit) => unit
143+
131144
/**
132145
`map(value, f)` returns `f(value)` if `value` is not `null`, otherwise returns
133146
`value` unchanged.

Diff for: src/Core__Nullable.mjs

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ function getExn(value) {
3838
};
3939
}
4040

41+
function forEach(value, f) {
42+
if (!(value == null)) {
43+
return Curry._1(f, value);
44+
}
45+
46+
}
47+
4148
function map(value, f) {
4249
if (value == null) {
4350
return value;
@@ -73,6 +80,7 @@ export {
7380
getOr ,
7481
getWithDefault ,
7582
getExn ,
83+
forEach ,
7684
map ,
7785
mapOr ,
7886
mapWithDefault ,

Diff for: src/Core__Nullable.res

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ let getExn: t<'a> => 'a = value =>
3434

3535
external getUnsafe: t<'a> => 'a = "%identity"
3636

37+
let forEach = (value, f) =>
38+
switch value->toOption {
39+
| Some(x) => f(x)
40+
| None => ()
41+
}
42+
3743
let map = (value, f) =>
3844
switch value->toOption {
3945
| Some(x) => make(f(x))

Diff for: src/Core__Nullable.resi

+14
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ Nullable.getUnsafe(Nullable.null) // Raises an error
138138
*/
139139
external getUnsafe: t<'a> => 'a = "%identity"
140140

141+
/**
142+
`forEach(value, f)` call `f` on `value`. if `value` is not `null` or `undefined`,
143+
then if calls `f`, otherwise returns `unit`.
144+
145+
## Examples
146+
147+
```rescript
148+
Nullable.forEach(Nullable.make("thing"), x => Console.log(x)) // logs "thing"
149+
Nullable.forEach(Nullable.null, x => Console.log(x)) // returns ()
150+
Nullable.forEach(undefined, x => Console.log(x)) // returns ()
151+
```
152+
*/
153+
let forEach: (t<'a>, 'a => unit) => unit
154+
141155
/**
142156
`map(value, f)` returns `f(value)` if `value` is not `null` or `undefined`,
143157
otherwise returns `value` unchanged.

0 commit comments

Comments
 (0)