aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcin Puc <marcin.e.puc@gmail.com>2021-03-15 16:05:08 +0100
committerLouis Pilfold <louis@lpil.uk>2021-04-16 19:05:30 +0100
commite4ef54b8c2bed679bf4a493718385d312cfc2409 (patch)
treefe34d6a571eb34c1118373dc6c39dc5309a5ed8a /src
parent27b31089fef8dc18f5ea33b35a64527ec511ab94 (diff)
downloadgleam_stdlib-e4ef54b8c2bed679bf4a493718385d312cfc2409.tar.gz
gleam_stdlib-e4ef54b8c2bed679bf4a493718385d312cfc2409.zip
Add iterator.interleave
Diffstat (limited to 'src')
-rw-r--r--src/gleam/iterator.gleam30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam
index daadea0..10f51a8 100644
--- a/src/gleam/iterator.gleam
+++ b/src/gleam/iterator.gleam
@@ -997,3 +997,33 @@ pub fn once(f: fn() -> element) -> Iterator(element) {
pub fn single(elem: element) -> Iterator(element) {
once(fn() { elem })
}
+
+fn do_interleave(
+ current: fn() -> Action(element),
+ next: fn() -> Action(element),
+) -> Action(element) {
+ case current() {
+ Stop -> next()
+ Continue(e, next_other) ->
+ Continue(e, fn() { do_interleave(next, next_other) })
+ }
+}
+
+/// Creates an iterator that alternates between the two given iterators
+/// until both have run out.
+///
+/// ## Examples
+///
+/// > from_list([1, 2, 3, 4]) |> interleave(from_list([11, 12, 13, 14])) |> to_list
+/// [1, 11, 2, 12, 3, 13, 4, 14]
+///
+/// > from_list([1, 2, 3, 4]) |> interleave(from_list([100])) |> to_list
+/// [1, 100, 2, 3, 4]
+///
+pub fn interleave(
+ left: Iterator(element),
+ with right: Iterator(element),
+) -> Iterator(element) {
+ fn() { do_interleave(left.continuation, right.continuation) }
+ |> Iterator
+}