Skip to content

Commit 3d40ff8

Browse files
authored
iterator.each (#431)
1 parent 909cfc9 commit 3d40ff8

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- The `iterator` module gains the `each` function.
6+
37
## v0.28.0 - 2023-03-26
48

59
- `regex.scan` now behaves consistently across both targets when a capture group does not capture anything.

src/gleam/iterator.gleam

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,3 +1417,26 @@ pub fn length(over iterator: Iterator(e)) -> Int {
14171417
iterator.continuation
14181418
|> do_length(0)
14191419
}
1420+
1421+
/// Traverse an iterator, calling a function on each element.
1422+
///
1423+
/// ## Examples
1424+
///
1425+
/// ```gleam
1426+
/// > empty() |> each(io.println)
1427+
/// Nil
1428+
/// ```
1429+
///
1430+
/// ```gleam
1431+
/// > from_list(["Tom", "Malory", "Louis"]) |> each(io.println)
1432+
/// // -> Tom
1433+
/// // -> Malory
1434+
/// // -> Louis
1435+
/// Nil
1436+
/// ```
1437+
///
1438+
pub fn each(over iterator: Iterator(a), with f: fn(a) -> b) -> Nil {
1439+
iterator
1440+
|> map(f)
1441+
|> run
1442+
}

test/gleam/iterator_test.gleam

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,9 @@ pub fn length_test() {
584584
|> iterator.length
585585
|> should.equal(0)
586586
}
587+
588+
pub fn each_test() {
589+
use it <- iterator.each(iterator.from_list([1]))
590+
it
591+
|> should.equal(1)
592+
}

0 commit comments

Comments
 (0)