Skip to content

Commit 950309b

Browse files
Bart SchuurmansBartSchuurmans
Bart Schuurmans
authored andcommitted
Add Dict.mapValues
1 parent e5431cd commit 950309b

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/Core__Dict.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,18 @@ function forEachWithKey(dict, f) {
1919
});
2020
}
2121

22+
function mapValues(dict, f) {
23+
var target = {};
24+
forEachWithKey(dict, (function (value, key) {
25+
target[key] = Curry._1(f, value);
26+
}));
27+
return target;
28+
}
29+
2230
export {
2331
$$delete$1 as $$delete,
2432
forEach ,
2533
forEachWithKey ,
34+
mapValues ,
2635
}
2736
/* No side effect */

src/Core__Dict.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ let forEachWithKey = (dict, f) => {
3737
f(value, key)
3838
})
3939
}
40+
41+
let mapValues = (dict, f) => {
42+
let target = make()
43+
dict->forEachWithKey((value, key) => {
44+
target->set(key, f(value))
45+
})
46+
target
47+
}

src/Core__Dict.resi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,17 @@ dict->Dict.forEachWithKey((value, key) => {
219219
```
220220
*/
221221
let forEachWithKey: (t<'a>, ('a, string) => unit) => unit
222+
223+
/**
224+
`mapValues(dictionary, f)` returns a new dictionary with the same keys, and `f` applied to each value in the original dictionary.
225+
226+
## Examples
227+
228+
```rescript
229+
let dict = Dict.fromArray([("key1", 1), ("key2", 2)])
230+
231+
dict->Dict.mapValues(v => v + 10)->Dict.toArray // [("key1", 11), ("key2", 12)]
232+
dict->Dict.mapValues(Int.toString)->Dict.toArray // [("key1", "1"), ("key2", "2")]
233+
```
234+
*/
235+
let mapValues: (t<'a>, 'a => 'b) => t<'b>

0 commit comments

Comments
 (0)