summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library/scala/Iterable.scala29
-rw-r--r--src/library/scala/Iterator.scala42
-rw-r--r--src/library/scala/Seq.scala22
-rw-r--r--test/files/run/iterators.check2
-rw-r--r--test/files/run/iterators.scala14
5 files changed, 86 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.
*
diff --git a/test/files/run/iterators.check b/test/files/run/iterators.check
index 03e648876f..399324347f 100644
--- a/test/files/run/iterators.check
+++ b/test/files/run/iterators.check
@@ -7,4 +7,6 @@ test check_drop was successful
test check_foreach was successful
test check_forall was successful
test check_fromArray was successful
+test check_indexOf was successful
+test check_findIndexOf was successful
diff --git a/test/files/run/iterators.scala b/test/files/run/iterators.scala
index a002ffadf0..9c427521df 100644
--- a/test/files/run/iterators.scala
+++ b/test/files/run/iterators.scala
@@ -84,6 +84,18 @@ object Test {
xs0.length + xs1.length + xs2.length + xs3.length + xs4.length
}
+ def check_indexOf: String = {
+ val i = List(1, 2, 3, 4, 5).indexOf(4)
+ val j = List(1, 2, 3, 4, 5).indexOf(16)
+ "" + i + "x" + j
+ }
+
+ def check_findIndexOf: String = {
+ val i = List(1, 2, 3, 4, 5).findIndexOf { x: Int => x >= 4 }
+ val j = List(1, 2, 3, 4, 5).findIndexOf { x: Int => x >= 16 }
+ "" + i + "x" + j
+ }
+
def check_success[A](name: String, closure: => A, expected: A) {
print("test " + name)
try {
@@ -110,6 +122,8 @@ object Test {
check_success("check_foreach", check_foreach, 190)
check_success("check_forall", check_forall, 0)
check_success("check_fromArray",check_fromArray, 14)
+ check_success("check_indexOf", check_indexOf, "3x-1")
+ check_success("check_findIndexOf", check_findIndexOf, "3x-1")
println()
}
}