diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-03-15 14:45:33 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-03-15 14:45:33 +0000 |
commit | 505bbf0b346b7b0cb1b18cb965f47df382f8217a (patch) | |
tree | 5e7fb4502b7262db71c06c829d2aea73ad70452a /src/library/scala/collection/TraversableLike.scala | |
parent | f1e0c8f025b3a5227d4e3c3fb39e9c8de3429e8c (diff) | |
download | scala-505bbf0b346b7b0cb1b18cb965f47df382f8217a.tar.gz scala-505bbf0b346b7b0cb1b18cb965f47df382f8217a.tar.bz2 scala-505bbf0b346b7b0cb1b18cb965f47df382f8217a.zip |
Fixes #3091. Review by community.
Diffstat (limited to 'src/library/scala/collection/TraversableLike.scala')
-rw-r--r-- | src/library/scala/collection/TraversableLike.scala | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala index 29d3c874f6..9d65e2c224 100644 --- a/src/library/scala/collection/TraversableLike.scala +++ b/src/library/scala/collection/TraversableLike.scala @@ -612,6 +612,46 @@ self => def reduceRightOption[B >: A](op: (A, B) => B): Option[B] = if (isEmpty) None else Some(reduceRight(op)) + /** + * Produces a collection containing cummulative results of applying the operator going left to right. + * $willNotTerminateInf + * $orderDependent + * + * @tparam B the type of the elements in the resulting collection + * @tparam That the actual type of the resulting collection + * @param z the initial value + * @param op the binary operator applied to the intermediate result and the element + * @param bf $bfinfo + * @return collection with intermediate results + */ + def scanLeft[B, That](z: B)(op: (B, A) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { + val (_, b) = foldLeft(z, bf(repr) += z) { (acc, x) => + val next = op(acc._1, x) + (next, acc._2 += next) + } + b.result + } + + /** + * Produces a collection containing cummulative results of applying the operator going right to left. + * $willNotTerminateInf + * $orderDependent + * + * @tparam B the type of the elements in the resulting collection + * @tparam That the actual type of the resulting collection + * @param z the initial value + * @param op the binary operator applied to the intermediate result and the element + * @param bf $bfinfo + * @return collection with intermediate results + */ + def scanRight[B, That](z: B)(op: (A, B) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { + val (_, b) = foldRight(z, bf(repr) += z) { (x, acc) => + val next = op(x, acc._1) + (next, acc._2 += next) + } + b.result + } + /** Sums up the elements of this collection. * * @param num an implicit parameter defining a set of numeric operations |