From 7a298200c79cbe0f4c378a60d54845cf6be696fb Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Fri, 19 Jan 2024 10:14:17 +0100 Subject: [PATCH 1/3] Add Dict.forEach and Dict.forEachWithKey --- src/Core__Dict.mjs | 14 ++++++++++++++ src/Core__Dict.res | 8 ++++++++ src/Core__Dict.resi | 30 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/Core__Dict.mjs b/src/Core__Dict.mjs index 8ac967f1..c6f375aa 100644 --- a/src/Core__Dict.mjs +++ b/src/Core__Dict.mjs @@ -5,7 +5,21 @@ 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]); + }); +} + export { $$delete$1 as $$delete, + forEach , + forEachWithKey , } /* No side effect */ diff --git a/src/Core__Dict.res b/src/Core__Dict.res index 3623a827..ffcc360a 100644 --- a/src/Core__Dict.res +++ b/src/Core__Dict.res @@ -23,3 +23,11 @@ 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)) +} diff --git a/src/Core__Dict.resi b/src/Core__Dict.resi index e3ac21f4..0fe5cc9c 100644 --- a/src/Core__Dict.resi +++ b/src/Core__Dict.resi @@ -190,3 +190,33 @@ 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 From 36076469633930d7f5e0df1c82cf740dcd1c04da Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Fri, 19 Jan 2024 10:28:11 +0100 Subject: [PATCH 2/3] Add Dict.mapValues --- src/Core__Dict.mjs | 9 +++++++++ src/Core__Dict.res | 8 ++++++++ src/Core__Dict.resi | 14 ++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/Core__Dict.mjs b/src/Core__Dict.mjs index c6f375aa..7ff37132 100644 --- a/src/Core__Dict.mjs +++ b/src/Core__Dict.mjs @@ -17,9 +17,18 @@ function forEachWithKey(dict, f) { }); } +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 */ diff --git a/src/Core__Dict.res b/src/Core__Dict.res index ffcc360a..1ea4bab7 100644 --- a/src/Core__Dict.res +++ b/src/Core__Dict.res @@ -31,3 +31,11 @@ let forEach = (dict, f) => { 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 +} diff --git a/src/Core__Dict.resi b/src/Core__Dict.resi index 0fe5cc9c..5ca553e1 100644 --- a/src/Core__Dict.resi +++ b/src/Core__Dict.resi @@ -220,3 +220,17 @@ dict->Dict.forEachWithKey((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> From a97dea9bad6880892ac68dcc348cfd276f332fa1 Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Wed, 14 Feb 2024 20:01:02 +0000 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de05e192..e7c4d996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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+.