diff options
author | stepancheg <stepancheg@epfl.ch> | 2008-05-24 15:01:02 +0000 |
---|---|---|
committer | stepancheg <stepancheg@epfl.ch> | 2008-05-24 15:01:02 +0000 |
commit | f6056a24c5a276cd8b763992f56e122f4ddbf76d (patch) | |
tree | e744d8d3548f5b7f4f5c5e0c88c15a16e02538f8 /src/library | |
parent | ad451f4a55ad9cd3683ee571a9b4697ddb4d1bfa (diff) | |
download | scala-f6056a24c5a276cd8b763992f56e122f4ddbf76d.tar.gz scala-f6056a24c5a276cd8b763992f56e122f4ddbf76d.tar.bz2 scala-f6056a24c5a276cd8b763992f56e122f4ddbf76d.zip |
Implement #886: indexOf, findIndexOf
introduces Iterator indexOf, findIndexOf
moved indexOf, findIndexOf bodies from Iterable to Iterator
deprecated Iterable indexOf, findIndexOf
introdued Seq indexOf, findIndexOf
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Iterable.scala | 29 | ||||
-rw-r--r-- | src/library/scala/Iterator.scala | 42 | ||||
-rw-r--r-- | src/library/scala/Seq.scala | 22 |
3 files changed, 70 insertions, 23 deletions
diff --git a/src/library/scala/Iterable.scala b/src/library/scala/Iterable.scala index 6063acf43a..511b9d0c48 100644 --- a/src/library/scala/Iterable.scala +++ b/src/library/scala/Iterable.scala @@ -291,17 +291,10 @@ trait Iterable[+A] { * @param p the predicate * @return the index of the first element satisfying <code>p</code>, * or -1 if such an element does not exist + * @deprecated Method is pushed to <code>Seq</code>, will be removed from <code>Iterable</code>. */ - def findIndexOf(p: A => Boolean): Int = { - val it = elements - var i = 0 - while (it.hasNext) - if (p(it.next)) - return i - else - i = i + 1 - return -1 - } + @deprecated def findIndexOf(p: A => Boolean): Int = + elements.findIndexOf(p) /** Returns the index of the first occurence of the specified * object in this iterable object. @@ -311,20 +304,10 @@ trait Iterable[+A] { * @return the index in this sequence of the first occurence of the * specified element, or -1 if the sequence does not contain * this element. + * @deprecated Method is pushed to <code>Seq</code>, will be removed from <code>Iterable</code>. */ - def indexOf[B >: A](elem: B): Int = { - val it = elements - var i = 0 - var found = false - while (!found && it.hasNext) { - if (it.next == elem) { - found = true - } else { - i = i + 1 - } - } - if (found) i else -1 - } + @deprecated def indexOf[B >: A](elem: B): Int = + elements.indexOf(elem) /** Combines the elements of this iterable object together using the binary * function <code>f</code>, from left to right, and starting with diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index 72b687c12a..fe57853d4f 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -446,6 +446,48 @@ trait Iterator[+A] { res } + /** Returns index of the first element satisying a predicate, or -1. + * + * @note may not terminate for infinite-sized collections. + * @param p the predicate + * @return the index of the first element satisfying <code>p</code>, + * or -1 if such an element does not exist + */ + def findIndexOf(p: A => Boolean): Int = { + var i = 0 + var found = false + while (!found && hasNext) { + if (p(next)) { + found = true + } else { + i += 1 + } + } + if (found) i else -1 + } + + /** Returns the index of the first occurence of the specified + * object in this iterable object. + * + * @note may not terminate for infinite-sized collections. + * @param elem element to search for. + * @return the index in this sequence of the first occurence of the + * specified element, or -1 if the sequence does not contain + * this element. + */ + def indexOf[B >: A](elem: B): Int = { + var i = 0 + var found = false + while (!found && hasNext) { + if (next == elem) { + found = true + } else { + i += 1 + } + } + if (found) i else -1 + } + /** Combines the elements of this iterator together using the binary * operator <code>op</code>, from left to right, and starting with * the value <code>z</code>. diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala index d43f2d9586..c3720427e1 100644 --- a/src/library/scala/Seq.scala +++ b/src/library/scala/Seq.scala @@ -238,6 +238,28 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] { if (found) i else -1 } + /** Returns index of the first element satisying a predicate, or -1. + * + * @note may not terminate for infinite-sized collections. + * @param p the predicate + * @return the index of the first element satisfying <code>p</code>, + * or -1 if such an element does not exist + */ + override def findIndexOf(p: A => Boolean): Int = + elements.findIndexOf(p) + + /** Returns the index of the first occurence of the specified + * object in this iterable object. + * + * @note may not terminate for infinite-sized collections. + * @param elem element to search for. + * @return the index in this sequence of the first occurence of the + * specified element, or -1 if the sequence does not contain + * this element. + */ + override def indexOf[B >: A](elem: B): Int = + elements.indexOf(elem) + /** Returns the sequence resulting from applying the given function * <code>f</code> to each element of this sequence. * |