diff options
author | Marcin Puc <marcin.e.puc@gmail.com> | 2021-03-09 14:37:11 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-03-10 10:44:17 +0100 |
commit | 1f693fae46e607d6ba811f7d01a9b903f0762420 (patch) | |
tree | 11fbe2b4fbdd579a1b1ebbb6bbe277e54076fb0e /src | |
parent | ddca8841995183f9ddba68e0686e702a18f56ad6 (diff) | |
download | gleam_stdlib-1f693fae46e607d6ba811f7d01a9b903f0762420.tar.gz gleam_stdlib-1f693fae46e607d6ba811f7d01a9b903f0762420.zip |
Add iterator.zip
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/iterator.gleam | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index 9146833..010d82a 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -534,3 +534,33 @@ pub fn iterate( ) -> Iterator(element) { unfold(initial, fn(element) { Next(element, f(element)) }) } + +fn do_zip( + left: fn() -> Action(a), + right: fn() -> Action(b), +) -> fn() -> Action(tuple(a, b)) { + fn() { + case left() { + Stop -> Stop + Continue(el_left, next_left) -> + case right() { + Stop -> Stop + Continue(el_right, next_right) -> + Continue(tuple(el_left, el_right), do_zip(next_left, next_right)) + } + } + } +} + +/// Zips two iterators together, emitting values from both +/// until the shorter one runs out. +/// +/// ## Examples +/// +/// > from_list(["a", "b", "c"]) |> zip(range(20, 30)) |> to_list +/// [tuple("a", 20), tuple("b", 21), tuple("c", 22)] +/// +pub fn zip(left: Iterator(a), right: Iterator(b)) -> Iterator(tuple(a, b)) { + do_zip(left.continuation, right.continuation) + |> Iterator +} |