diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/iterator.gleam | 23 |
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 +} |