From 505bbf0b346b7b0cb1b18cb965f47df382f8217a Mon Sep 17 00:00:00 2001 From: Aleksandar Pokopec Date: Mon, 15 Mar 2010 14:45:33 +0000 Subject: Fixes #3091. Review by community. --- src/library/scala/collection/TraversableLike.scala | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src') 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 -- cgit v1.2.3