Skip to content

Commit 4c8df1a

Browse files
shardulcSethTisue
authored andcommitted
Scaladoc: Fix underspecn. of folds, reduces re. associativity and ordering
1 parent 6da44c5 commit 4c8df1a

File tree

3 files changed

+233
-153
lines changed

3 files changed

+233
-153
lines changed

library/src/scala/collection/ArrayOps.scala

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -761,20 +761,19 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
761761
true
762762
}
763763

764-
/** Applies a binary operator to a start value and all elements of this array,
765-
* going left to right.
766-
*
767-
* @param z the start value.
768-
* @param op the binary operator.
769-
* @tparam B the result type of the binary operator.
770-
* @return the result of inserting `op` between consecutive elements of this array,
771-
* going left to right with the start value `z` on the left:
772-
* {{{
773-
* op(...op(z, x_1), x_2, ..., x_n)
774-
* }}}
775-
* where `x,,1,,, ..., x,,n,,` are the elements of this array.
776-
* Returns `z` if this array is empty.
777-
*/
764+
/** Applies the given binary operator `op` to the given initial value `z` and
765+
* all elements of this array, going left to right. Returns the initial value
766+
* if this array is empty.
767+
*
768+
* If `x,,1,,`, `x,,2,,`, ..., `x,,n,,` are the elements of this array, the
769+
* result is `op( op( ... op( op(z, x,,1,,), x,,2,,) ... ), x,,n,,)`.
770+
*
771+
* @param z An initial value.
772+
* @param op A binary operator.
773+
* @tparam B The result type of the binary operator.
774+
* @return The result of applying `op` to `z` and all elements of this array,
775+
* going left to right. Returns `z` if this array is empty.
776+
*/
778777
def foldLeft[B](z: B)(op: (B, A) => B): B = {
779778
def f[@specialized(Specializable.Everything) T](xs: Array[T], op: (Any, Any) => Any, z: Any): Any = {
780779
val length = xs.length
@@ -867,20 +866,20 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
867866
res
868867
}
869868

870-
/** Applies a binary operator to all elements of this array and a start value,
871-
* going right to left.
872-
*
873-
* @param z the start value.
874-
* @param op the binary operator.
875-
* @tparam B the result type of the binary operator.
876-
* @return the result of inserting `op` between consecutive elements of this array,
877-
* going right to left with the start value `z` on the right:
878-
* {{{
879-
* op(x_1, op(x_2, ... op(x_n, z)...))
880-
* }}}
881-
* where `x,,1,,, ..., x,,n,,` are the elements of this array.
882-
* Returns `z` if this array is empty.
883-
*/
869+
/** Applies the given binary operator `op` to all elements of this array and
870+
* the given initial value `z`, going right to left. Returns the initial
871+
* value if this array is empty.
872+
*
873+
* If `x,,1,,`, `x,,2,,`, ..., `x,,n,,` are the elements of this array, the
874+
* result is `op(x,,1,,, op(x,,2,,, op( ... op(x,,n,,, z) ... )))`.
875+
*
876+
* @param z An initial value.
877+
* @param op A binary operator.
878+
* @tparam B The result type of the binary operator.
879+
* @return The result of applying `op` to all elements of this array
880+
* and `z`, going right to left. Returns `z` if this array
881+
* is empty.
882+
*/
884883
def foldRight[B](z: B)(op: (A, B) => B): B = {
885884
def f[@specialized(Specializable.Everything) T](xs: Array[T], op: (Any, Any) => Any, z: Any): Any = {
886885
var v = z
@@ -907,15 +906,17 @@ final class ArrayOps[A](private val xs: Array[A]) extends AnyVal {
907906

908907
}
909908

910-
/** Folds the elements of this array using the specified associative binary operator.
911-
*
912-
* @tparam A1 a type parameter for the binary operator, a supertype of `A`.
913-
* @param z a neutral element for the fold operation; may be added to the result
914-
* an arbitrary number of times, and must not change the result (e.g., `Nil` for list concatenation,
915-
* 0 for addition, or 1 for multiplication).
916-
* @param op a binary operator that must be associative.
917-
* @return the result of applying the fold operator `op` between all the elements, or `z` if this array is empty.
918-
*/
909+
/** Alias for [[foldLeft]].
910+
*
911+
* The type parameter is more restrictive than for `foldLeft` to be
912+
* consistent with [[IterableOnceOps.fold]].
913+
*
914+
* @tparam A1 The type parameter for the binary operator, a supertype of `A`.
915+
* @param z An initial value.
916+
* @param op A binary operator.
917+
* @return The result of applying `op` to `z` and all elements of this array,
918+
* going left to right. Returns `z` if this string is empty.
919+
*/
919920
def fold[A1 >: A](z: A1)(op: (A1, A1) => A1): A1 = foldLeft(z)(op)
920921

921922
/** Builds a new array by applying a function to all elements of this array.

0 commit comments

Comments
 (0)