Skip to content

Commit 246f9a3

Browse files
BartSchuurmansillusionalsagacity
authored andcommitted
Dict: add forEach, forEachWithKey, mapValues (rescript-lang#181)
* Add Dict.forEach and Dict.forEachWithKey * Add Dict.mapValues * Update CHANGELOG
1 parent e1beba2 commit 246f9a3

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Next version
44

55
- BREAKING: Fixes the type of `RegExp.Result.t` to be `array<option<string>>` instead of `array<string>`. https://github.com/rescript-association/rescript-core/pull/233.
6+
- Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181
67

78
## 0.7.0
89

src/Core__Dict.mjs

+23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,30 @@ function $$delete$1(dict, string) {
55
delete(dict[string]);
66
}
77

8+
function forEach(dict, f) {
9+
Object.values(dict).forEach(function (value) {
10+
f(value);
11+
});
12+
}
13+
14+
function forEachWithKey(dict, f) {
15+
Object.entries(dict).forEach(function (param) {
16+
f(param[1], param[0]);
17+
});
18+
}
19+
20+
function mapValues(dict, f) {
21+
var target = {};
22+
forEachWithKey(dict, (function (value, key) {
23+
target[key] = f(value);
24+
}));
25+
return target;
26+
}
27+
828
export {
929
$$delete$1 as $$delete,
30+
forEach ,
31+
forEachWithKey ,
32+
mapValues ,
1033
}
1134
/* No side effect */

src/Core__Dict.res

+16
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ 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+
}
34+
35+
let mapValues = (dict, f) => {
36+
let target = make()
37+
dict->forEachWithKey((. value, key) => {
38+
target->set(key, f(. value))
39+
})
40+
target
41+
}

src/Core__Dict.resi

+44
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,47 @@ 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
223+
224+
/**
225+
`mapValues(dictionary, f)` returns a new dictionary with the same keys, and `f` applied to each value in the original dictionary.
226+
227+
## Examples
228+
229+
```rescript
230+
let dict = Dict.fromArray([("key1", 1), ("key2", 2)])
231+
232+
dict->Dict.mapValues(v => v + 10)->Dict.toArray // [("key1", 11), ("key2", 12)]
233+
dict->Dict.mapValues(Int.toString)->Dict.toArray // [("key1", "1"), ("key2", "2")]
234+
```
235+
*/
236+
let mapValues: (t<'a>, (. 'a) => 'b) => t<'b>

0 commit comments

Comments
 (0)