diff options
author | drew <drew@drewolson.org> | 2020-11-03 07:22:37 -0600 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-11-03 13:25:01 +0000 |
commit | e1dce822e1ff14664a30b429b5e2e2cbf3748221 (patch) | |
tree | 0c940f1d641fac784d7dd120414de29e4b80b2f9 /src | |
parent | cbac55e668d7fc7df601b604e741b1baa64c7db8 (diff) | |
download | gleam_stdlib-e1dce822e1ff14664a30b429b5e2e2cbf3748221.tar.gz gleam_stdlib-e1dce822e1ff14664a30b429b5e2e2cbf3748221.zip |
Add `step` eliminator for iterators
The `step` function allows eager access to the first item in the
iterator, but does not force the rest of the iterator.
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/iterator.gleam | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index bfc8455..e75b0a7 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -168,6 +168,34 @@ pub fn to_list(iterator: Iterator(element)) -> List(element) { |> list.reverse } +/// Eagerly access the first value of an interator, returning a `Next` +/// that contains the first value and the rest of the iterator. +/// +/// If called on an empty iterator, `Done` is returned. +/// +/// ## Examples +/// +/// > assert Next(head, tail) = +/// > [1, 2, 3, 4] +/// > |> from_list +/// > |> step +/// > head +/// 1 +/// > tail |> to_list +/// [2, 3, 4] +/// +/// > [] +/// > |> from_list +/// > |> step +/// Done +/// +pub fn step(iterator: Iterator(e)) -> Step(e, Iterator(e)) { + case iterator.continuation() { + Stop -> Done + Continue(e, a) -> Next(e, Iterator(a)) + } +} + fn do_take( continuation: fn() -> Action(e), desired: Int, |