summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
Diffstat (limited to 'sources')
-rw-r--r--sources/scala/Iterator.scala10
1 files changed, 10 insertions, 0 deletions
diff --git a/sources/scala/Iterator.scala b/sources/scala/Iterator.scala
index 89c6507e66..57a97ac441 100644
--- a/sources/scala/Iterator.scala
+++ b/sources/scala/Iterator.scala
@@ -7,6 +7,16 @@ trait Iterator[a] {
def foreach(f: a => Unit): Unit =
while (hasNext) { f(next) }
+ def take(n: Int) = new Iterator[a] {
+ var remaining = n;
+ def hasNext = remaining > 0 && Iterator.this.hasNext;
+ def next: a =
+ if (hasNext) { remaining = remaining - 1; Iterator.this.next }
+ else error("next on empty iterator");
+ }
+
+ def drop(n: Int): Iterator[a] = if (n > 0) { next; drop(n - 1); } else this;
+
def map[b](f: a => b): Iterator[b] = new Iterator[b] {
def hasNext = Iterator.this.hasNext;
def next = f(Iterator.this.next)