diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/VectorLike.scala | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/src/library/scala/collection/VectorLike.scala b/src/library/scala/collection/VectorLike.scala index d4b2ca1d4c..c0331d9758 100644 --- a/src/library/scala/collection/VectorLike.scala +++ b/src/library/scala/collection/VectorLike.scala @@ -13,6 +13,7 @@ package scala.collection import generic._ import mutable.ArrayBuffer +import scala.annotation.tailrec /** Sequences that support O(1) element access and O(1) length computation. * This class does not add any methods to Seq but overrides several @@ -77,27 +78,22 @@ trait VectorLike[+A, +Repr] extends SeqLike[A, Repr] { self => if (i < length) Some(this(i)) else None } - private def foldl[B](start: Int, z: B, op: (B, A) => B): B = { - var i = start - val len = length - var result = z - while (i < len) { - result = op(result, this(i)) - i += 1 - } - result - } + @tailrec + private def foldl[B](start: Int, end: Int, z: B, op: (B, A) => B): B = + if (start == end) z + else foldl(start + 1, end, op(z, this(start)), op) - private def foldr[B](start: Int, len: Int, z: B, op: (A, B) => B): B = - if (start == len) z - else op(this(start), foldr(start + 1, len, z, op)) + @tailrec + private def foldr[B](start: Int, end: Int, z: B, op: (A, B) => B): B = + if (start == end) z + else foldr(start, end - 1, op(this(end - 1), z), op) override def foldLeft[B](z: B)(op: (B, A) => B): B = - foldl(0, z, op) + foldl(0, length, z, op) override def foldRight[B](z: B)(op: (A, B) => B): B = foldr(0, length, z, op) override def reduceLeft[B >: A](op: (B, A) => B): B = - if (length > 0) foldl(1, this(0), op) else super.reduceLeft(op) + if (length > 0) foldl(1, length, this(0), op) else super.reduceLeft(op) override def reduceRight[B >: A](op: (A, B) => B): B = if (length > 0) foldr(0, length - 1, this(length - 1), op) else super.reduceRight(op) |