diff options
author | Marcin Puc <marcin.e.puc@gmail.com> | 2021-03-14 10:56:35 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-03-21 12:46:10 +0000 |
commit | 6906334a71f1ee202c69b0c58596c550d2691061 (patch) | |
tree | 277703b3c4497766ee997406170be0271cda403d | |
parent | 1d267f4180a4c018dea38d7f6124f7af10ca3005 (diff) | |
download | gleam_stdlib-6906334a71f1ee202c69b0c58596c550d2691061.tar.gz gleam_stdlib-6906334a71f1ee202c69b0c58596c550d2691061.zip |
Rearrange helper functions in iterator
-rw-r--r-- | src/gleam/iterator.gleam | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index 881a46d..b506b78 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -550,39 +550,6 @@ fn do_take_while( } } -fn do_scan( - continuation: fn() -> Action(element), - f: fn(element, acc) -> acc, - accumulator: acc, -) -> fn() -> Action(acc) { - fn() { - case continuation() { - Stop -> Stop - Continue(el, next) -> { - let accumulated = f(el, accumulator) - Continue(accumulated, do_scan(next, f, accumulated)) - } - } - } -} - -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)) - } - } - } -} - /// Creates an iterator that yields elements while the predicate returns `True`. /// /// ## Examples @@ -629,6 +596,22 @@ pub fn drop_while( |> Iterator } +fn do_scan( + continuation: fn() -> Action(element), + f: fn(element, acc) -> acc, + accumulator: acc, +) -> fn() -> Action(acc) { + fn() { + case continuation() { + Stop -> Stop + Continue(el, next) -> { + let accumulated = f(el, accumulator) + Continue(accumulated, do_scan(next, f, accumulated)) + } + } + } +} + /// Creates an iterator from an existing iterator and a stateful function. /// /// Specifically, this behaves like `fold`, but yields intermediate results. @@ -649,6 +632,23 @@ pub fn scan( |> Iterator } +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. /// |