aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKayla Washburn <mckayla@hey.com>2023-04-04 09:48:23 -0600
committerGitHub <noreply@github.com>2023-04-04 16:48:23 +0100
commit3d40ff82b6e68b89c31f1326cdc3db5a8928a6f7 (patch)
treead7d7897ed9dc4844c1fc4cbc1140b058bd51fcd /src
parent909cfc9878f39b312e86e2d888b9c523a8dc67cf (diff)
downloadgleam_stdlib-3d40ff82b6e68b89c31f1326cdc3db5a8928a6f7.tar.gz
gleam_stdlib-3d40ff82b6e68b89c31f1326cdc3db5a8928a6f7.zip
`iterator.each` (#431)
Diffstat (limited to 'src')
-rw-r--r--src/gleam/iterator.gleam23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam
index 1486ae3..65e3e29 100644
--- a/src/gleam/iterator.gleam
+++ b/src/gleam/iterator.gleam
@@ -1417,3 +1417,26 @@ pub fn length(over iterator: Iterator(e)) -> Int {
iterator.continuation
|> do_length(0)
}
+
+/// Traverse an iterator, calling a function on each element.
+///
+/// ## Examples
+///
+/// ```gleam
+/// > empty() |> each(io.println)
+/// Nil
+/// ```
+///
+/// ```gleam
+/// > from_list(["Tom", "Malory", "Louis"]) |> each(io.println)
+/// // -> Tom
+/// // -> Malory
+/// // -> Louis
+/// Nil
+/// ```
+///
+pub fn each(over iterator: Iterator(a), with f: fn(a) -> b) -> Nil {
+ iterator
+ |> map(f)
+ |> run
+}