@@ -49,6 +49,22 @@ import scala.language.higherKinds
49
49
*/
50
50
trait GenTraversableOnce [+ A ] extends Any {
51
51
52
+ /** Applies a function `f` to all elements of this $coll.
53
+ *
54
+ * @param f the function that is applied for its side-effect to every element.
55
+ * The result of function `f` is discarded.
56
+ *
57
+ * @tparam U the type parameter describing the result of function `f`.
58
+ * This result will always be ignored. Typically `U` is `Unit`,
59
+ * but this is not necessary.
60
+ *
61
+ * @usecase def foreach(f: A => Unit): Unit
62
+ * @inheritdoc
63
+ *
64
+ * Note: this method underlies the implementation of most other bulk operations.
65
+ * It's important to implement this method in an efficient way.
66
+ *
67
+ */
52
68
def foreach [U ](f : A => U ): Unit
53
69
54
70
def hasDefiniteSize : Boolean
@@ -110,13 +126,14 @@ trait GenTraversableOnce[+A] extends Any {
110
126
* binary operator.
111
127
*
112
128
* $undefinedorder
129
+ * $willNotTerminateInf
113
130
*
114
131
* @tparam A1 a type parameter for the binary operator, a supertype of `A`.
115
132
* @param z a neutral element for the fold operation; may be added to the result
116
133
* an arbitrary number of times, and must not change the result (e.g., `Nil` for list concatenation,
117
- * 0 for addition, or 1 for multiplication.)
118
- * @param op a binary operator that must be associative
119
- * @return the result of applying fold operator `op` between all the elements and `z`
134
+ * 0 for addition, or 1 for multiplication).
135
+ * @param op a binary operator that must be associative.
136
+ * @return the result of applying the fold operator `op` between all the elements and `z`, or `z` if this $coll is empty.
120
137
*/
121
138
def fold [A1 >: A ](z : A1 )(op : (A1 , A1 ) => A1 ): A1
122
139
@@ -205,6 +222,7 @@ trait GenTraversableOnce[+A] extends Any {
205
222
* op(...op(z, x_1), x_2, ..., x_n)
206
223
* }}}
207
224
* where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
225
+ * Returns `z` if this $coll is empty.
208
226
*/
209
227
def foldLeft [B ](z : B )(op : (B , A ) => B ): B
210
228
@@ -222,30 +240,32 @@ trait GenTraversableOnce[+A] extends Any {
222
240
* op(x_1, op(x_2, ... op(x_n, z)...))
223
241
* }}}
224
242
* where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
243
+ * Returns `z` if this $coll is empty.
225
244
*/
226
245
def foldRight [B ](z : B )(op : (A , B ) => B ): B
227
246
228
247
/** Aggregates the results of applying an operator to subsequent elements.
229
248
*
230
- * This is a more general form of `fold` and `reduce`. It has similar
231
- * semantics, but does not require the result to be a supertype of the
232
- * element type. It traverses the elements in different partitions
233
- * sequentially, using `seqop` to update the result, and then applies
234
- * `combop` to results from different partitions. The implementation of
235
- * this operation may operate on an arbitrary number of collection
236
- * partitions, so `combop` may be invoked an arbitrary number of times.
237
- *
238
- * For example, one might want to process some elements and then produce
239
- * a `Set`. In this case, `seqop` would process an element and append it
240
- * to the list, while `combop` would concatenate two lists from different
241
- * partitions together. The initial value `z` would be an empty set.
249
+ * This is a more general form of `fold` and `reduce`. It is similar to
250
+ * `foldLeft` in that it doesn't require the result to be a supertype of the
251
+ * element type. In addition, it allows parallel collections to be processed
252
+ * in chunks, and then combines the intermediate results.
253
+ *
254
+ * `aggregate` splits the $coll into partitions and processes each
255
+ * partition by sequentially applying `seqop`, starting with `z` (like
256
+ * `foldLeft`). Those intermediate results are then combined by using
257
+ * `combop` (like `fold`). The implementation of this operation may operate
258
+ * on an arbitrary number of collection partitions (even 1), so `combop` may
259
+ * be invoked an arbitrary number of times (even 0).
260
+ *
261
+ * As an example, consider summing up the integer values of a list of chars.
262
+ * The initial value for the sum is 0. First, `seqop` transforms each input
263
+ * character to an Int and adds it to the sum (of the partition). Then,
264
+ * `combop` just needs to sum up the intermediate results of the partitions:
242
265
* {{{
243
- * pc .aggregate(Set[Int]())(_ += process(_), _ ++ _ )
266
+ * List('a', 'b', 'c') .aggregate(0)({ (sum, ch) => sum + ch.toInt }, { (p1, p2) => p1 + p2 } )
244
267
* }}}
245
268
*
246
- * Another example is calculating geometric mean from a collection of doubles
247
- * (one would typically require big doubles for this).
248
- *
249
269
* @tparam B the type of accumulated results
250
270
* @param z the initial value for the accumulated result of the partition - this
251
271
* will typically be the neutral element for the `seqop` operator (e.g.
@@ -423,13 +443,13 @@ trait GenTraversableOnce[+A] extends Any {
423
443
*/
424
444
def find (@ deprecatedName(' pred ) p : A => Boolean ): Option [A ]
425
445
426
- /** Copies values of this $coll to an array.
446
+ /** Copies the elements of this $coll to an array.
427
447
* Fills the given array `xs` with values of this $coll.
428
448
* Copying will stop once either the end of the current $coll is reached,
429
- * or the end of the array is reached.
449
+ * or the end of the target array is reached.
430
450
*
431
451
* @param xs the array to fill.
432
- * @tparam B the type of the elements of the array.
452
+ * @tparam B the type of the elements of the target array.
433
453
*
434
454
* @usecase def copyToArray(xs: Array[A]): Unit
435
455
* @inheritdoc
@@ -438,14 +458,14 @@ trait GenTraversableOnce[+A] extends Any {
438
458
*/
439
459
def copyToArray [B >: A ](xs : Array [B ]): Unit
440
460
441
- /** Copies values of this $coll to an array.
461
+ /** Copies the elements of this $coll to an array.
442
462
* Fills the given array `xs` with values of this $coll, beginning at index `start`.
443
463
* Copying will stop once either the end of the current $coll is reached,
444
- * or the end of the array is reached.
464
+ * or the end of the target array is reached.
445
465
*
446
466
* @param xs the array to fill.
447
467
* @param start the starting index.
448
- * @tparam B the type of the elements of the array.
468
+ * @tparam B the type of the elements of the target array.
449
469
*
450
470
* @usecase def copyToArray(xs: Array[A], start: Int): Unit
451
471
* @inheritdoc
@@ -454,6 +474,22 @@ trait GenTraversableOnce[+A] extends Any {
454
474
*/
455
475
def copyToArray [B >: A ](xs : Array [B ], start : Int ): Unit
456
476
477
+ /** Copies the elements of this $coll to an array.
478
+ * Fills the given array `xs` with at most `len` elements of
479
+ * this $coll, starting at position `start`.
480
+ * Copying will stop once either the end of the current $coll is reached,
481
+ * or the end of the target array is reached, or `len` elements have been copied.
482
+ *
483
+ * @param xs the array to fill.
484
+ * @param start the starting index.
485
+ * @param len the maximal number of elements to copy.
486
+ * @tparam B the type of the elements of the target array.
487
+ *
488
+ * @usecase def copyToArray(xs: Array[A], start: Int, len: Int): Unit
489
+ * @inheritdoc
490
+ *
491
+ * $willNotTerminateInf
492
+ */
457
493
def copyToArray [B >: A ](xs : Array [B ], start : Int , len : Int ): Unit
458
494
459
495
/** Displays all elements of this $coll in a string using start, end, and
0 commit comments