Skip to content

Commit e07ad91

Browse files
Bart SchuurmansBartSchuurmans
Bart Schuurmans
authored andcommitted
Add Dict.forEach and Dict.forEachWithKey
1 parent 532af3a commit e07ad91

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/Core__Dict.mjs

+13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3+
import * as Curry from "rescript/lib/es6/curry.js";
34

45
function $$delete$1(dict, string) {
56
delete(dict[string]);
67
}
78

9+
function forEach(dict, f) {
10+
Object.values(dict).forEach(Curry.__1(f));
11+
}
12+
13+
function forEachWithKey(dict, f) {
14+
Object.entries(dict).forEach(function (param) {
15+
Curry._2(f, param[1], param[0]);
16+
});
17+
}
18+
819
export {
920
$$delete$1 as $$delete,
21+
forEach ,
22+
forEachWithKey ,
1023
}
1124
/* No side effect */

src/Core__Dict.res

+8
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ let delete = (dict, string) => {
2323
@val external assign: (t<'a>, t<'a>) => t<'a> = "Object.assign"
2424

2525
@val external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign"
26+
27+
let forEach = (dict, f) => {
28+
dict->valuesToArray->Core__Array.forEach(value => f(value))
29+
}
30+
31+
let forEachWithKey = (dict, f) => {
32+
dict->toArray->Core__Array.forEach(((key, value)) => f(value, key))
33+
}

src/Core__Dict.resi

+30
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,33 @@ Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray)
190190
*/
191191
@val
192192
external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign"
193+
194+
/**
195+
`forEach(dictionary, f)` iterates through all values of the dict.
196+
197+
> Please note that this is *without the keys*, just the values. If you need the key as well, use `Dict.forEachWithKey`.
198+
199+
## Examples
200+
```rescript
201+
let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])
202+
203+
dict->Dict.forEach(value => {
204+
Console.log(value)
205+
})
206+
```
207+
*/
208+
let forEach: (t<'a>, 'a => unit) => unit
209+
210+
/**
211+
`forEachWithKey(dictionary, f)` iterates through all values of the dict, including the key for each value.
212+
213+
## Examples
214+
```rescript
215+
let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])
216+
217+
dict->Dict.forEachWithKey((value, key) => {
218+
Console.log2(value, key)
219+
})
220+
```
221+
*/
222+
let forEachWithKey: (t<'a>, ('a, string) => unit) => unit

test/ArrayTests.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ Test.run([
450450
Test.run([
451451
[
452452
"ArrayTests.res",
453-
109,
453+
112,
454454
20,
455455
39
456456
],
@@ -464,7 +464,7 @@ Test.run([
464464
Test.run([
465465
[
466466
"ArrayTests.res",
467-
110,
467+
113,
468468
20,
469469
34
470470
],

0 commit comments

Comments
 (0)