summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGeoffrey Washburn <geoffrey.washburn@epfl.ch>2008-04-04 09:40:48 +0000
committerGeoffrey Washburn <geoffrey.washburn@epfl.ch>2008-04-04 09:40:48 +0000
commitb5dae302418fee4b3d6492ad1027650032a133c3 (patch)
treec075dd8b2dde2f38dba54add27458f59905ffa53 /src
parent2677581b24454e8d320e50f48022dc71f4f6a4fe (diff)
downloadscala-b5dae302418fee4b3d6492ad1027650032a133c3.tar.gz
scala-b5dae302418fee4b3d6492ad1027650032a133c3.tar.bz2
scala-b5dae302418fee4b3d6492ad1027650032a133c3.zip
Changed signature on reduceLeft/Right per ticke...
Changed signature on reduceLeft/Right per ticket #721.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/Iterable.scala4
-rw-r--r--src/library/scala/IterableProxy.scala4
-rw-r--r--src/library/scala/Iterator.scala4
-rw-r--r--src/library/scala/List.scala16
4 files changed, 18 insertions, 10 deletions
diff --git a/src/library/scala/Iterable.scala b/src/library/scala/Iterable.scala
index 9cf18f3b46..c52257e120 100644
--- a/src/library/scala/Iterable.scala
+++ b/src/library/scala/Iterable.scala
@@ -369,7 +369,7 @@ trait Iterable[+A] {
* <code>a<sub>0</sub>, a<sub>1</sub>, ..., a<sub>n</sub></code>.
* @throws Predef.UnsupportedOperationException if the iterable object is empty.
*/
- def reduceLeft[B >: A](op: (B, B) => B): B = elements.reduceLeft(op)
+ def reduceLeft[B >: A](op: (B, A) => B): B = elements.reduceLeft(op)
/** Combines the elements of this iterable object together using the binary
* operator <code>op</code>, from right to left
@@ -382,7 +382,7 @@ trait Iterable[+A] {
*
* @throws Predef.UnsupportedOperationException if the iterator is empty.
*/
- def reduceRight[B >: A](op: (B, B) => B): B = elements.reduceRight(op)
+ def reduceRight[B >: A](op: (A, B) => B): B = elements.reduceRight(op)
/** Copy all elements to a given buffer
* @note Will not terminate for infinite-sized collections.
diff --git a/src/library/scala/IterableProxy.scala b/src/library/scala/IterableProxy.scala
index f20c3d25ca..1c51ca627a 100644
--- a/src/library/scala/IterableProxy.scala
+++ b/src/library/scala/IterableProxy.scala
@@ -44,8 +44,8 @@ trait IterableProxy[+A] extends Iterable[A] with Proxy {
override def foldRight[B](z: B)(op: (A, B) => B): B = (self foldRight z)(op)
override def /:[B](z: B)(op: (B, A) => B): B = (z /: self)(op)
override def :\[B](z: B)(op: (A, B) => B): B = (self :\ z)(op)
- override def reduceLeft[B >: A](op: (B, B) => B): B = self reduceLeft op
- override def reduceRight[B >: A](op: (B, B) => B): B = self reduceRight op
+ override def reduceLeft[B >: A](op: (B, A) => B): B = self reduceLeft op
+ override def reduceRight[B >: A](op: (A, B) => B): B = self reduceRight op
override def sameElements[B >: A](that: Iterable[B]): Boolean = self sameElements that
override def copyToBuffer[B >: A](dest: Buffer[B]): Unit = self copyToBuffer dest
override def toList: List[A] = self.toList
diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala
index 1f75ba85aa..db6d682db0 100644
--- a/src/library/scala/Iterator.scala
+++ b/src/library/scala/Iterator.scala
@@ -505,7 +505,7 @@ trait Iterator[+A] {
* @throws Predef.UnsupportedOperationException if the iterator is empty.
*/
@throws(classOf[UnsupportedOperationException])
- def reduceLeft[B >: A](op: (B, B) => B): B = {
+ def reduceLeft[B >: A](op: (B, A) => B): B = {
if (hasNext) foldLeft[B](next)(op)
else throw new UnsupportedOperationException("empty.reduceLeft")
}
@@ -521,7 +521,7 @@ trait Iterator[+A] {
* @throws Predef.UnsupportedOperationException if the iterator is empty.
*/
@throws(classOf[UnsupportedOperationException])
- def reduceRight[B >: A](op: (B, B) => B): B = {
+ def reduceRight[B >: A](op: (A, B) => B): B = {
if (!hasNext) throw new UnsupportedOperationException("empty.reduceRight")
val x = next
if (hasNext) op(x, reduceRight(op))
diff --git a/src/library/scala/List.scala b/src/library/scala/List.scala
index b6086e2460..f87b034cde 100644
--- a/src/library/scala/List.scala
+++ b/src/library/scala/List.scala
@@ -1036,9 +1036,17 @@ sealed abstract class List[+A] extends Seq[A] {
* <code>a<sub>0</sub>, a<sub>1</sub>, ..., a<sub>n</sub></code>.
* @throws Predef.UnsupportedOperationException if the list is empty.
*/
- override def reduceLeft[B >: A](f: (B, B) => B): B = this match {
+ override def reduceLeft[B >: A](f: (B, A) => B): B = this match {
case Nil => throw new UnsupportedOperationException("Nil.reduceLeft")
- case x :: xs => ((xs: List[B]) foldLeft (x: B))(f)
+ case x :: Nil => x
+ case x0 :: x1 :: xs =>
+ var acc : B = f(x0, x1)
+ var these : List[A] = xs
+ while (!these.isEmpty) {
+ acc = f(acc, these.head)
+ these = these.tail
+ }
+ acc
}
/** Combines the elements of this list together using the binary
@@ -1051,9 +1059,9 @@ sealed abstract class List[+A] extends Seq[A] {
*
* @throws Predef.UnsupportedOperationException if the list is empty.
*/
- override def reduceRight[B >: A](f: (B, B) => B): B = this match {
+ override def reduceRight[B >: A](f: (A, B) => B): B = this match {
case Nil => throw new UnsupportedOperationException("Nil.reduceRight")
- case x :: Nil => x: B
+ case x :: Nil => x
case x :: xs => f(x, xs reduceRight f)
}