From 1f5bd8a590e9c29fdaabb53adeae1bf5c22258f4 Mon Sep 17 00:00:00 2001 From: Lex Spoon Date: Fri, 5 May 2006 11:49:08 +0000 Subject: added zipWithIndex to several collection classes --- src/library/scala/Array.scala | 2 ++ src/library/scala/Iterator.scala | 19 +++++++++++++++++++ src/library/scala/List.scala | 22 ++++++++++++++++++++++ src/library/scala/Stream.scala | 23 +++++++++++++++++++++++ src/library/scala/runtime/BoxedArray.scala | 24 ++++++++++++++++++++++++ 5 files changed, 90 insertions(+) (limited to 'src/library') diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index b684647bf3..2820a018cf 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -71,4 +71,6 @@ final class Array[a](_length: Int) extends Seq[a] { def filter(p: a => Boolean): Array[a] = throw new Error(); def map[b](f: a => b): Array[b] = throw new Error(); def flatMap[b](f: a => Array[b]): Array[b] = throw new Error(); + def zip[b](that: Array[b]): Array[Tuple2[a,b]] = throw new Error(); + def zipWithIndex: Array[Tuple2[a,Int]] = throw new Error(); } diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index e86e07381e..8d6de4871e 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -277,6 +277,25 @@ trait Iterator[+A] { def next = Pair(Iterator.this.next, that.next); } + + /** Return an iterator that pairs each element of this iterator + * with its index, counting from 0. + * + * @param start the index of the first element + * + * @return an iterator yielding (a0,0), (a0,1)... + * where ai are the elements from this iterator. + */ + def zipWithIndex = new Iterator[Pair[A, int]] { + var idx = 0 + def hasNext = Iterator.this.hasNext + def next = { + val ret = Pair(Iterator.this.next, idx) + idx = idx + 1 + ret + } + } + /** Apply a function f to all elements of this * iterable object. * diff --git a/src/library/scala/List.scala b/src/library/scala/List.scala index 395f497558..205ffc75c6 100644 --- a/src/library/scala/List.scala +++ b/src/library/scala/List.scala @@ -958,6 +958,28 @@ sealed abstract class List[+a] extends Seq[a] { b.toList } + /** Return an list that pairs each element of this list + * with its index, counting from 0. + * + * @param start the index of the first element + * + * @return an iterator yielding (a0,0), (a0,1)... + * where ai are the elements from this iterator. + */ + def zipWithIndex = { + val b = new ListBuffer[Pair[a,int]] + var these = this + var idx = 0 + + while(!these.isEmpty) { + b += Pair(these.head, idx) + these = these.tail + idx = idx + 1 + } + + b.toList + } + /** Returns a list formed from this list and the specified list * that by associating each element of the former with * the element at the same position in the latter. diff --git a/src/library/scala/Stream.scala b/src/library/scala/Stream.scala index 42669be6d2..4f4f65b183 100644 --- a/src/library/scala/Stream.scala +++ b/src/library/scala/Stream.scala @@ -101,6 +101,26 @@ object Stream { else cons(lo, loop(step(lo))); loop(start) } + + /** + * Create an infinite stream starting at start + * and incrementing by step step + * + * @param start the start value of the stream + * @param step the increment value of the stream + * @return the stream starting at value start. + */ + def from(start: Int, step: Int): Stream[Int] = + cons(start, from(start+step, step)) + + /** + * Create an infinite stream starting at start + * and incrementing by 1. + * + * @param start the start value of the stream + * @return the stream starting at value start. + */ + def from(start: Int): Stream[Int] = from(start, 1) } /** @@ -231,6 +251,9 @@ trait Stream[+a] extends Seq[a] { if (this.isEmpty || that.isEmpty) Stream.empty else Stream.cons(Tuple2(this.head, that.head), this.tail.zip(that.tail)); + def zipWithIndex: Stream[Tuple2[a, int]] = + zip(Stream.from(0)) + def print: unit = if (isEmpty) Console.println("Stream.empty") else { diff --git a/src/library/scala/runtime/BoxedArray.scala b/src/library/scala/runtime/BoxedArray.scala index 4550be053d..8309143095 100644 --- a/src/library/scala/runtime/BoxedArray.scala +++ b/src/library/scala/runtime/BoxedArray.scala @@ -79,5 +79,29 @@ abstract class BoxedArray extends PartialFunction[Int, Object] with Seq[Object] Array.concat(tmp: _*) } + final def zip[b](that: Array[b]): Array[Tuple2[Object,b]] = { + val len = length + if(len != that.length) + throw new Error("zipping arrays of different length") + val result = new Array[Tuple2[Object,b]](len) + var i = 0 + while (i < len) { + result(i) = new Tuple2(this(i), that(i)) + i = i + 1 + } + result + } + + final def zipWithIndex: Array[Tuple2[Object,Int]] = { + val len = length + val result = new Array[Tuple2[Object,Int]](len) + var i = 0 + while (i < len) { + result(i) = new Tuple2(this(i), i) + i = i + 1 + } + result + } + override final def stringPrefix: String = "Array" } -- cgit v1.2.3