diff options
author | mihaylov <mihaylov@epfl.ch> | 2005-03-21 17:02:17 +0000 |
---|---|---|
committer | mihaylov <mihaylov@epfl.ch> | 2005-03-21 17:02:17 +0000 |
commit | 7179a093ef8f5c46941f6440704389316416b7f5 (patch) | |
tree | 380f65f1bc372ef6492d60bb5a6d1235aa7e17fc | |
parent | f0e63b8bcbc9ff6be4fe6d372ffa324f4ab3adf9 (diff) | |
download | scala-7179a093ef8f5c46941f6440704389316416b7f5.tar.gz scala-7179a093ef8f5c46941f6440704389316416b7f5.tar.bz2 scala-7179a093ef8f5c46941f6440704389316416b7f5.zip |
- Avoid reimplementing foreach, forall, exists,...
- Avoid reimplementing foreach, forall, exists, find, foldLeft,
foldRight by forwarding them to their implementation in scala.Iterator
-rw-r--r-- | sources/scala/Iterable.scala | 46 |
1 files changed, 6 insertions, 40 deletions
diff --git a/sources/scala/Iterable.scala b/sources/scala/Iterable.scala index 0be6cfa000..838ba02821 100644 --- a/sources/scala/Iterable.scala +++ b/sources/scala/Iterable.scala @@ -74,10 +74,7 @@ trait Iterable[+A] { * * @param f a function that is applied to every element. */ - def foreach(f: A => Unit): Unit = { - val it = elements; - while (it.hasNext) { f(it.next) } - } + def foreach(f: A => Unit): Unit = elements.foreach(f); /** Apply a predicate <code>p</code> to all elements of this * iterable object and return true, iff the predicate yields @@ -86,12 +83,7 @@ trait Iterable[+A] { * @param p the predicate * @returns true, iff the predicate yields true for all elements. */ - def forall(p: A => Boolean): Boolean = { - val it = elements; - var res = true; - while (res && it.hasNext) { res = p(it.next) } - res - } + def forall(p: A => Boolean): Boolean = elements.forall(p); /** Apply a predicate <code>p</code> to all elements of this * iterable object and return true, iff there is at least one @@ -100,12 +92,7 @@ trait Iterable[+A] { * @param p the predicate * @returns true, iff the predicate yields true for at least one element. */ - def exists(p: A => Boolean): Boolean = { - val it = elements; - var res = false; - while (!res && it.hasNext) { res = p(it.next) } - res - } + def exists(p: A => Boolean): Boolean = elements.exists(p); /** Find and return the first element of the iterable object satisfying a * predicate, if any. @@ -114,16 +101,7 @@ trait Iterable[+A] { * @return the first element in the iterable object satisfying <code>p</code>, * or <code>None</code> if none exists. */ - def find(p: A => Boolean): Option[A] = { - val it = elements; - var res: Option[A] = None; - while (res.isEmpty && it.hasNext) { - val e = it.next; - if (p(e)) - res = Some(e); - } - res - } + def find(p: A => Boolean): Option[A] = elements.find(p); /** Combines the elements of this list together using the binary * operator <code>op</code>, from left to right, and starting with @@ -131,14 +109,7 @@ trait Iterable[+A] { * @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list * is <code>List(a0, a1, ..., an)</code>. */ - def foldLeft[B](z: B)(op: (B, A) => B): B = { - val it = elements; - var acc = z; - while (it.hasNext) { - acc = op(acc, it.next) - } - acc - } + def foldLeft[B](z: B)(op: (B, A) => B): B = elements.foldLeft(z)(op); /** Combines the elements of this list together using the binary * operator <code>op</code>, from rigth to left, and starting with @@ -146,12 +117,7 @@ trait Iterable[+A] { * @return <code>a0 op (... op (an op z)...)</code> if the list * is <code>[a0, a1, ..., an]</code>. */ - def foldRight[B](z: B)(op: (A, B) => B): B = { - val it = elements; - def fold(z: B): B = - if (it.hasNext) op(it.next, fold(z)) else z; - fold(z) - } + def foldRight[B](z: B)(op: (A, B) => B): B = elements.foldRight(z)(op); /** Similar to <code>foldLeft</code> but can be used as * an operator with the order of list and zero arguments reversed. |