aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormpatajac <patajacmatija3@gmail.com>2024-10-30 19:12:37 +0100
committerLouis Pilfold <louis@lpil.uk>2024-10-30 19:58:22 +0000
commit9eee271fdae645e7a249ea866394edb9fdf5c664 (patch)
tree2bd27eecee344fdef15aeb558b4b840a90ff780e /src
parent7a710f3f12114d6a5729673860acb8936ed3b13d (diff)
downloadgleam_stdlib-9eee271fdae645e7a249ea866394edb9fdf5c664.tar.gz
gleam_stdlib-9eee271fdae645e7a249ea866394edb9fdf5c664.zip
The `set` module gains the `each` function
Diffstat (limited to 'src')
-rw-r--r--src/gleam/set.gleam27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/gleam/set.gleam b/src/gleam/set.gleam
index 5dfc749..a5e3918 100644
--- a/src/gleam/set.gleam
+++ b/src/gleam/set.gleam
@@ -380,3 +380,30 @@ pub fn symmetric_difference(
minus: intersection(of: first, and: second),
)
}
+
+/// Calls a function for each member in a set, discarding the return
+/// value.
+///
+/// Useful for producing a side effect for every item of a set.
+///
+/// ```gleam
+/// import gleam/io
+///
+/// let set = from_list(["apple", "banana", "cherry"])
+///
+/// each(set, io.println)
+/// // -> Nil
+/// // apple
+/// // banana
+/// // cherry
+/// ```
+///
+/// The order of elements in the iteration is an implementation detail that
+/// should not be relied upon.
+///
+pub fn each(set: Set(member), fun: fn(member) -> a) -> Nil {
+ fold(set, Nil, fn(nil, member) {
+ fun(member)
+ nil
+ })
+}