aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcin Puc <marcin.e.puc@gmail.com>2021-04-13 16:44:35 +0200
committerLouis Pilfold <louis@lpil.uk>2021-04-16 14:42:54 +0100
commitc23299f135d49d2b2579d83699312c1085f20d25 (patch)
tree38ff56811d61630c52c16c87d3a51b7c36545cd7 /src
parentf4c08abeda2050f6614e7c963bf15c3abaca2d7f (diff)
downloadgleam_stdlib-c23299f135d49d2b2579d83699312c1085f20d25.tar.gz
gleam_stdlib-c23299f135d49d2b2579d83699312c1085f20d25.zip
Add iterator.{empty, once, single}
Diffstat (limited to 'src')
-rw-r--r--src/gleam/iterator.gleam34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam
index 0b747df..c09901c 100644
--- a/src/gleam/iterator.gleam
+++ b/src/gleam/iterator.gleam
@@ -965,3 +965,37 @@ pub fn last(iterator: Iterator(element)) -> Result(element, Nil) {
iterator
|> reduce(fn(elem, _) { elem })
}
+
+/// Creates an iterator that yields no elements.
+///
+/// ## Examples
+///
+/// > empty() |> to_list
+/// []
+///
+pub fn empty() -> Iterator(element) {
+ Iterator(stop)
+}
+
+/// Creates an iterator that yields exactly one element provided by calling the given function.
+///
+/// ## Examples
+///
+/// > once(fn() { 1 }) |> to_list
+/// [1]
+///
+pub fn once(f: fn() -> element) -> Iterator(element) {
+ fn() { Continue(f(), stop) }
+ |> Iterator
+}
+
+/// Creates an iterator that yields the given element exactly once.
+///
+/// ## Examples
+///
+/// > single(1) |> to_list
+/// [1]
+///
+pub fn single(elem: element) -> Iterator(element) {
+ once(fn() { elem })
+}