aboutsummaryrefslogtreecommitdiff
path: root/std/default/iterators.pk
diff options
context:
space:
mode:
Diffstat (limited to 'std/default/iterators.pk')
-rw-r--r--std/default/iterators.pk38
1 files changed, 38 insertions, 0 deletions
diff --git a/std/default/iterators.pk b/std/default/iterators.pk
new file mode 100644
index 0000000..f97c61a
--- /dev/null
+++ b/std/default/iterators.pk
@@ -0,0 +1,38 @@
+## std/iterators: The Iter interface and associated functions.
+## This module is imported by default.
+
+## The Iter interface. Any type implementing `next()` is iterable.
+pub type Iter[T] = interface
+ next(mut Self): Option[T]
+
+# todo: useful functions for an iterator
+# https://doc.rust-lang.org/std/iter/trait.Iterator.html#provided-methods
+
+pub func advance_by[T](self: Iter[T], n: uint): Result[T, ...] =
+ for i in 0 .. n:
+ if self.next().is_none():
+ return Error(...)
+ Okay
+
+pub func get[T](self: Iter[T], at: uint): Option[T]
+ self.advance_by(at-1).ok?
+ self.next()
+
+# todo: implement iter[T](self: ...): Iter[T] funcs
+# todo: efficient functional methods
+
+## The Peek interface. Any type implementing Iter, `peek`, and `peek_nth` is peekable.
+pub type Peek[T] = interface
+ next(mut Self): Option[T]
+ peek(mut Self): Option[T]
+ peek_nth(mut Self, n: uint): Option[T]
+
+# todo: implement peek[T](self: Iter[T]): Peek[T]
+# todo: implement Peekable struct
+# https://github.com/LukeMathWalker/multipeek/blob/main/src/lib.rs
+
+## We don't want a Countable. It's not terribly useful.
+# pub type Countable[T] = interface
+# next(mut Self): Option[T]
+# len(Self): uint
+# get(Self, uint): Option[T]