diff options
author | Lex Spoon <lex@lexspoon.org> | 2006-05-05 11:49:08 +0000 |
---|---|---|
committer | Lex Spoon <lex@lexspoon.org> | 2006-05-05 11:49:08 +0000 |
commit | 1f5bd8a590e9c29fdaabb53adeae1bf5c22258f4 (patch) | |
tree | f8d798227ef16cca1b0c91d0680f05f849516cc2 /src/library | |
parent | 5a94352a620b652bafe8386adf2637f4bec86e25 (diff) | |
download | scala-1f5bd8a590e9c29fdaabb53adeae1bf5c22258f4.tar.gz scala-1f5bd8a590e9c29fdaabb53adeae1bf5c22258f4.tar.bz2 scala-1f5bd8a590e9c29fdaabb53adeae1bf5c22258f4.zip |
added zipWithIndex to several collection classes
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Array.scala | 2 | ||||
-rw-r--r-- | src/library/scala/Iterator.scala | 19 | ||||
-rw-r--r-- | src/library/scala/List.scala | 22 | ||||
-rw-r--r-- | src/library/scala/Stream.scala | 23 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedArray.scala | 24 |
5 files changed, 90 insertions, 0 deletions
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 <code>start</code> the index of the first element + * + * @return an iterator yielding <code>(a0,0), (a0,1)...</code> + * where <code>ai</code> 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 <code>f</code> 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 <code>start</code> the index of the first element + * + * @return an iterator yielding <code>(a0,0), (a0,1)...</code> + * where <code>ai</code> 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 * <code>that</code> 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 <code>start</code> + * and incrementing by step <code>step</code> + * + * @param start the start value of the stream + * @param step the increment value of the stream + * @return the stream starting at value <code>start</code>. + */ + def from(start: Int, step: Int): Stream[Int] = + cons(start, from(start+step, step)) + + /** + * Create an infinite stream starting at <code>start</code> + * and incrementing by 1. + * + * @param start the start value of the stream + * @return the stream starting at value <code>start</code>. + */ + 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" } |