Skip to content

Dict: add forEach, forEachWithKey, mapValues #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Next version

- Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181

## 1.0.0

- Up ReScript dependency to 11+.
Expand Down
23 changes: 23 additions & 0 deletions src/Core__Dict.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,30 @@ function $$delete$1(dict, string) {
delete(dict[string]);
}

function forEach(dict, f) {
Object.values(dict).forEach(function (value) {
f(value);
});
}

function forEachWithKey(dict, f) {
Object.entries(dict).forEach(function (param) {
f(param[1], param[0]);
});
}

function mapValues(dict, f) {
var target = {};
forEachWithKey(dict, (function (value, key) {
target[key] = f(value);
}));
return target;
}

export {
$$delete$1 as $$delete,
forEach ,
forEachWithKey ,
mapValues ,
}
/* No side effect */
16 changes: 16 additions & 0 deletions src/Core__Dict.res
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ let delete = (dict, string) => {
@val external assign: (t<'a>, t<'a>) => t<'a> = "Object.assign"

@val external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign"

let forEach = (dict, f) => {
dict->valuesToArray->Core__Array.forEach(value => f(value))
}

let forEachWithKey = (dict, f) => {
dict->toArray->Core__Array.forEach(((key, value)) => f(value, key))
}

let mapValues = (dict, f) => {
let target = make()
dict->forEachWithKey((value, key) => {
target->set(key, f(value))
})
target
}
44 changes: 44 additions & 0 deletions src/Core__Dict.resi
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,47 @@ Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray)
*/
@val
external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign"

/**
`forEach(dictionary, f)` iterates through all values of the dict.

> Please note that this is *without the keys*, just the values. If you need the key as well, use `Dict.forEachWithKey`.

## Examples
```rescript
let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])

dict->Dict.forEach(value => {
Console.log(value)
})
```
*/
let forEach: (t<'a>, 'a => unit) => unit

/**
`forEachWithKey(dictionary, f)` iterates through all values of the dict, including the key for each value.

## Examples
```rescript
let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])

dict->Dict.forEachWithKey((value, key) => {
Console.log2(value, key)
})
```
*/
let forEachWithKey: (t<'a>, ('a, string) => unit) => unit

/**
`mapValues(dictionary, f)` returns a new dictionary with the same keys, and `f` applied to each value in the original dictionary.

## Examples

```rescript
let dict = Dict.fromArray([("key1", 1), ("key2", 2)])

dict->Dict.mapValues(v => v + 10)->Dict.toArray // [("key1", 11), ("key2", 12)]
dict->Dict.mapValues(Int.toString)->Dict.toArray // [("key1", "1"), ("key2", "2")]
```
*/
let mapValues: (t<'a>, 'a => 'b) => t<'b>