Skip to content

Commit 2218497

Browse files
committed
Merge pull request scala#4760 from janekdb/2.11.x-collection-documentation-fixes-previously-4651
2.11.x collection documentation fixes
2 parents e10413e + e3cbcd5 commit 2218497

File tree

5 files changed

+85
-88
lines changed

5 files changed

+85
-88
lines changed

src/library/scala/collection/GenTraversableLike.scala

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,6 @@ trait GenTraversableLike[+A, +Repr] extends Any with GenTraversableOnce[A] with
158158
@migration("The behavior of `scanRight` has changed. The previous behavior can be reproduced with scanRight.reverse.", "2.9.0")
159159
def scanRight[B, That](z: B)(op: (A, B) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That
160160

161-
/** Applies a function `f` to all elements of this $coll.
162-
*
163-
* @param f the function that is applied for its side-effect to every element.
164-
* The result of function `f` is discarded.
165-
*
166-
* @tparam U the type parameter describing the result of function `f`.
167-
* This result will always be ignored. Typically `U` is `Unit`,
168-
* but this is not necessary.
169-
*
170-
* @usecase def foreach(f: A => Unit): Unit
171-
* @inheritdoc
172-
*/
173161
def foreach[U](f: A => U): Unit
174162

175163
/** Builds a new collection by applying a function to all elements of this $coll.

src/library/scala/collection/GenTraversableOnce.scala

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ import scala.language.higherKinds
4949
*/
5050
trait GenTraversableOnce[+A] extends Any {
5151

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+
*/
5268
def foreach[U](f: A => U): Unit
5369

5470
def hasDefiniteSize: Boolean
@@ -110,13 +126,14 @@ trait GenTraversableOnce[+A] extends Any {
110126
* binary operator.
111127
*
112128
* $undefinedorder
129+
* $willNotTerminateInf
113130
*
114131
* @tparam A1 a type parameter for the binary operator, a supertype of `A`.
115132
* @param z a neutral element for the fold operation; may be added to the result
116133
* 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.
120137
*/
121138
def fold[A1 >: A](z: A1)(op: (A1, A1) => A1): A1
122139

@@ -205,6 +222,7 @@ trait GenTraversableOnce[+A] extends Any {
205222
* op(...op(z, x_1), x_2, ..., x_n)
206223
* }}}
207224
* where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
225+
* Returns `z` if this $coll is empty.
208226
*/
209227
def foldLeft[B](z: B)(op: (B, A) => B): B
210228

@@ -222,30 +240,32 @@ trait GenTraversableOnce[+A] extends Any {
222240
* op(x_1, op(x_2, ... op(x_n, z)...))
223241
* }}}
224242
* where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
243+
* Returns `z` if this $coll is empty.
225244
*/
226245
def foldRight[B](z: B)(op: (A, B) => B): B
227246

228247
/** Aggregates the results of applying an operator to subsequent elements.
229248
*
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:
242265
* {{{
243-
* pc.aggregate(Set[Int]())(_ += process(_), _ ++ _)
266+
* List('a', 'b', 'c').aggregate(0)({ (sum, ch) => sum + ch.toInt }, { (p1, p2) => p1 + p2 })
244267
* }}}
245268
*
246-
* Another example is calculating geometric mean from a collection of doubles
247-
* (one would typically require big doubles for this).
248-
*
249269
* @tparam B the type of accumulated results
250270
* @param z the initial value for the accumulated result of the partition - this
251271
* will typically be the neutral element for the `seqop` operator (e.g.
@@ -423,13 +443,13 @@ trait GenTraversableOnce[+A] extends Any {
423443
*/
424444
def find(@deprecatedName('pred) p: A => Boolean): Option[A]
425445

426-
/** Copies values of this $coll to an array.
446+
/** Copies the elements of this $coll to an array.
427447
* Fills the given array `xs` with values of this $coll.
428448
* 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.
430450
*
431451
* @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.
433453
*
434454
* @usecase def copyToArray(xs: Array[A]): Unit
435455
* @inheritdoc
@@ -438,14 +458,14 @@ trait GenTraversableOnce[+A] extends Any {
438458
*/
439459
def copyToArray[B >: A](xs: Array[B]): Unit
440460

441-
/** Copies values of this $coll to an array.
461+
/** Copies the elements of this $coll to an array.
442462
* Fills the given array `xs` with values of this $coll, beginning at index `start`.
443463
* 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.
445465
*
446466
* @param xs the array to fill.
447467
* @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.
449469
*
450470
* @usecase def copyToArray(xs: Array[A], start: Int): Unit
451471
* @inheritdoc
@@ -454,6 +474,22 @@ trait GenTraversableOnce[+A] extends Any {
454474
*/
455475
def copyToArray[B >: A](xs: Array[B], start: Int): Unit
456476

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+
*/
457493
def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Unit
458494

459495
/** Displays all elements of this $coll in a string using start, end, and

src/library/scala/collection/TraversableLike.scala

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,6 @@ trait TraversableLike[+A, +Repr] extends Any
340340
b.result
341341
}
342342

343-
/** Tests whether a predicate holds for all elements of this $coll.
344-
*
345-
* $mayNotTerminateInf
346-
*
347-
* @param p the predicate used to test elements.
348-
* @return `true` if this $coll is empty, otherwise `true` if the given predicate `p`
349-
* holds for all elements of this $coll, otherwise `false`.
350-
*/
351343
def forall(p: A => Boolean): Boolean = {
352344
var result = true
353345
breakable {
@@ -374,15 +366,6 @@ trait TraversableLike[+A, +Repr] extends Any
374366
result
375367
}
376368

377-
/** Finds the first element of the $coll satisfying a predicate, if any.
378-
*
379-
* $mayNotTerminateInf
380-
* $orderDependent
381-
*
382-
* @param p the predicate used to test elements.
383-
* @return an option value containing the first element in the $coll
384-
* that satisfies `p`, or `None` if none exists.
385-
*/
386369
def find(p: A => Boolean): Option[A] = {
387370
var result: Option[A] = None
388371
breakable {
@@ -594,23 +577,6 @@ trait TraversableLike[+A, +Repr] extends Any
594577
*/
595578
def inits: Iterator[Repr] = iterateUntilEmpty(_.init)
596579

597-
/** Copies elements of this $coll to an array.
598-
* Fills the given array `xs` with at most `len` elements of
599-
* this $coll, starting at position `start`.
600-
* Copying will stop once either the end of the current $coll is reached,
601-
* or the end of the array is reached, or `len` elements have been copied.
602-
*
603-
* @param xs the array to fill.
604-
* @param start the starting index.
605-
* @param len the maximal number of elements to copy.
606-
* @tparam B the type of the elements of the array.
607-
*
608-
*
609-
* @usecase def copyToArray(xs: Array[A], start: Int, len: Int): Unit
610-
* @inheritdoc
611-
*
612-
* $willNotTerminateInf
613-
*/
614580
def copyToArray[B >: A](xs: Array[B], start: Int, len: Int) {
615581
var i = start
616582
val end = (start + len) min xs.length
@@ -625,7 +591,7 @@ trait TraversableLike[+A, +Repr] extends Any
625591

626592
@deprecatedOverriding("Enforce contract of toTraversable that if it is Traversable it returns itself.", "2.11.0")
627593
def toTraversable: Traversable[A] = thisCollection
628-
594+
629595
def toIterator: Iterator[A] = toStream.iterator
630596
def toStream: Stream[A] = toBuffer.toStream
631597
// Override to provide size hint.

src/library/scala/collection/package.scala

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ package scala
1313
*
1414
* == Guide ==
1515
*
16-
* A detailed guide for the collections library is available
16+
* A detailed guide for using the collections library is available
1717
* at [[http://docs.scala-lang.org/overviews/collections/introduction.html]].
18+
* Developers looking to extend the collections library can find a description
19+
* of its architecture at
20+
* [[http://docs.scala-lang.org/overviews/core/architecture-of-scala-collections.html]].
1821
*
1922
* == Using Collections ==
2023
*
@@ -31,37 +34,38 @@ package scala
3134
* array: Array[Int] = Array(1, 2, 3, 4, 5, 6)
3235
*
3336
* scala> array map { _.toString }
34-
* res0: Array[java.lang.String] = Array(1, 2, 3, 4, 5, 6)
37+
* res0: Array[String] = Array(1, 2, 3, 4, 5, 6)
3538
*
3639
* scala> val list = List(1,2,3,4,5,6)
3740
* list: List[Int] = List(1, 2, 3, 4, 5, 6)
3841
*
3942
* scala> list map { _.toString }
40-
* res1: List[java.lang.String] = List(1, 2, 3, 4, 5, 6)
43+
* res1: List[String] = List(1, 2, 3, 4, 5, 6)
4144
*
4245
* }}}
4346
*
4447
* == Creating Collections ==
4548
*
46-
* The most common way to create a collection is to use the companion objects as factories.
47-
* Of these, the three most common
48-
* are [[scala.collection.Seq]], [[scala.collection.immutable.Set]], and [[scala.collection.immutable.Map]]. Their
49-
* companion objects are all available
50-
* as type aliases the either the [[scala]] package or in `scala.Predef`, and can be used
51-
* like so:
49+
* The most common way to create a collection is to use its companion object as
50+
* a factory. The three most commonly used collections are
51+
* [[scala.collection.Seq]], [[scala.collection.immutable.Set]], and
52+
* [[scala.collection.immutable.Map]].
53+
* They can be used directly as shown below since their companion objects are
54+
* all available as type aliases in either the [[scala]] package or in
55+
* `scala.Predef`. New collections are created like this:
5256
* {{{
5357
* scala> val seq = Seq(1,2,3,4,1)
5458
* seq: Seq[Int] = List(1, 2, 3, 4, 1)
5559
*
5660
* scala> val set = Set(1,2,3,4,1)
5761
* set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
5862
*
59-
* scala> val map = Map(1 -> "one",2 -> "two", 3 -> "three",2 -> "too")
60-
* map: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,too), (3,three))
63+
* scala> val map = Map(1 -> "one", 2 -> "two", 3 -> "three", 2 -> "too")
64+
* map: scala.collection.immutable.Map[Int,String] = Map(1 -> one, 2 -> too, 3 -> three)
6165
* }}}
6266
*
63-
* It is also typical to use the [[scala.collection.immutable]] collections over those
64-
* in [[scala.collection.mutable]]; The types aliased in
67+
* It is also typical to prefer the [[scala.collection.immutable]] collections
68+
* over those in [[scala.collection.mutable]]; the types aliased in
6569
* the `scala.Predef` object are the immutable versions.
6670
*
6771
* Also note that the collections library was carefully designed to include several implementations of
@@ -74,9 +78,13 @@ package scala
7478
*
7579
* === Converting between Java Collections ===
7680
*
77-
* The `JavaConversions` object provides implicit defs that will allow mostly seamless integration
78-
* between Java Collections-based APIs and the Scala collections library.
81+
* The [[scala.collection.JavaConversions]] object provides implicit defs that
82+
* will allow mostly seamless integration between APIs using Java Collections
83+
* and the Scala collections library.
7984
*
85+
* Alternatively the [[scala.collection.JavaConverters]] object provides a collection
86+
* of decorators that allow converting between Scala and Java collections using `asScala`
87+
* and `asJava` methods.
8088
*/
8189
package object collection {
8290
import scala.collection.generic.CanBuildFrom

src/library/scala/collection/parallel/ParIterableLike.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,5 +1499,4 @@ self: ParIterableLike[T, Repr, Sequential] =>
14991499
append(s)
15001500
}
15011501
})
1502-
15031502
}

0 commit comments

Comments
 (0)