aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRyan M. Moore <rmm1047@gmail.com>2024-05-15 14:59:51 -0400
committerLouis Pilfold <louis@lpil.uk>2024-05-17 23:15:59 +0100
commit43e32953ba28a892fe5063d60b253c2c7282cbaf (patch)
treefd083dee875f6546a9e036ed4c9d64a46dc7c8e2 /src
parent1d60b1842d4a27dc7403ae9c440c6b6f2264c255 (diff)
downloadgleam_stdlib-43e32953ba28a892fe5063d60b253c2c7282cbaf.tar.gz
gleam_stdlib-43e32953ba28a892fe5063d60b253c2c7282cbaf.zip
Add `dict.each`
Diffstat (limited to 'src')
-rw-r--r--src/gleam/dict.gleam29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/gleam/dict.gleam b/src/gleam/dict.gleam
index 8b459ec..6013f32 100644
--- a/src/gleam/dict.gleam
+++ b/src/gleam/dict.gleam
@@ -497,3 +497,32 @@ pub fn fold(
|> to_list
|> do_fold(initial, fun)
}
+
+/// Calls a function for each key and value in a dict, discarding the return
+/// value.
+///
+/// Useful for producing a side effect for every item of a dict.
+///
+/// ```gleam
+/// import gleam/io
+///
+/// let dict = from_list([#("a", "apple"), #("b", "banana"), #("c", "cherry")])
+///
+/// each(dict, fn(key, value) {
+/// io.println(key <> " => " <> value)
+/// })
+/// // -> Nil
+/// // a => apple
+/// // b => banana
+/// // c => cherry
+/// ```
+///
+/// The order of elements in the iteration is an implementation detail that
+/// should not be relied upon.
+///
+pub fn each(dict: Dict(k, v), fun: fn(k, v) -> b) -> Nil {
+ fold(dict, Nil, fn(nil, k, v) {
+ fun(k, v)
+ nil
+ })
+}