Skip to content

Commit 9f19404

Browse files
authored
Add code tabs for collections-2.13/introduction, overview and trait-iterable (#2571)
* Add code tabs for collections-2.13/introduction * Add code tabs for collections-2.13/overview * Add code tabs for collections-2.13/trait-iterable * Add code tabs for collections-2.13/introduction * Add code tabs for collections-2.13/overview * Add code tabs for collections-2.13/overview * Add code tabs for collections-2.13/trait-iterable * Add code tabs for collections-2.13/seqs * TraversableLike is deprecated.
1 parent cf34d4a commit 9f19404

File tree

4 files changed

+90
-42
lines changed

4 files changed

+90
-42
lines changed

_overviews/collections-2.13/introduction.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ permalink: /overviews/collections-2.13/:title.html
1212
---
1313

1414
The collections framework is the heart of the Scala 2.13 standard
15-
library. It provides a common, uniform, and all-encompassing
15+
library, also used in Scala 3.x. It provides a common, uniform, and all-encompassing
1616
framework for collection types. This framework enables you to work
1717
with data in memory at a high level, with the basic building blocks of
1818
a program being whole collections, instead of individual elements.
@@ -70,12 +70,18 @@ for arrays.
7070
**Example:** Here's one line of code that demonstrates many of the
7171
advantages of Scala's collections.
7272

73-
val (minors, adults) = people partition (_.age < 18)
73+
{% tabs introduction_1 %}
74+
{% tab 'Scala 2 and 3' for=introduction_1 %}
75+
```
76+
val (minors, adults) = people partition (_.age < 18)
77+
```
78+
{% endtab %}
79+
{% endtabs %}
7480

7581
It's immediately clear what this operation does: It partitions a
7682
collection of `people` into `minors` and `adults` depending on
7783
their age. Because the `partition` method is defined in the root
78-
collection type `TraversableLike`, this code works for any kind of
84+
collection type `IterableOps`, this code works for any kind of
7985
collection, including arrays. The resulting `minors` and `adults`
8086
collections will be of the same type as the `people` collection.
8187

_overviews/collections-2.13/overview.md

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ permalink: /overviews/collections-2.13/:title.html
1313
---
1414

1515
Scala collections systematically distinguish between mutable and
16-
immutable collections. A _mutable_ collection can be updated or
16+
immutable collections. A _mutable_ collection can be updated, reduced or
1717
extended in place. This means you can change, add, or remove elements
1818
of a collection as a side effect. _Immutable_ collections, by
1919
contrast, never change. You have still operations that simulate
@@ -43,7 +43,7 @@ A collection in package `scala.collection` can be either mutable or
4343
immutable. For instance, [collection.IndexedSeq\[T\]](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/IndexedSeq.html)
4444
is a superclass of both [collection.immutable.IndexedSeq\[T\]](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/immutable/IndexedSeq.html)
4545
and
46-
[collection.mutable.IndexedSeq\[T\]](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/mutable/IndexedSeq.html)
46+
[collection.mutable.IndexedSeq\[T\]](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/mutable/IndexedSeq.html).
4747
Generally, the root collections in
4848
package `scala.collection` support transformation operations
4949
affecting the whole collection, the immutable
@@ -73,7 +73,13 @@ A useful convention if you want to use both mutable and immutable
7373
versions of collections is to import just the package
7474
`collection.mutable`.
7575

76-
import scala.collection.mutable
76+
{% tabs overview_1 %}
77+
{% tab 'Scala 2 and 3' for=overview_1 %}
78+
```scala mdoc
79+
import scala.collection.mutable
80+
```
81+
{% endtab %}
82+
{% endtabs %}
7783

7884
Then a word like `Set` without a prefix still refers to an immutable collection,
7985
whereas `mutable.Set` refers to the mutable counterpart.
@@ -86,10 +92,16 @@ aliases in the `scala` package, so you can use them by their simple
8692
names without needing an import. An example is the `List` type, which
8793
can be accessed alternatively as
8894

89-
scala.collection.immutable.List // that's where it is defined
90-
scala.List // via the alias in the scala package
91-
List // because scala._
92-
// is always automatically imported
95+
{% tabs overview_2 %}
96+
{% tab 'Scala 2 and 3' for=overview_2 %}
97+
```scala mdoc
98+
scala.collection.immutable.List // that's where it is defined
99+
scala.List // via the alias in the scala package
100+
List // because scala._
101+
// is always automatically imported
102+
```
103+
{% endtab %}
104+
{% endtabs %}
93105

94106
Other types aliased are
95107
[Iterable](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/Iterable.html), [Seq](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/immutable/Seq.html), [IndexedSeq](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/immutable/IndexedSeq.html), [Iterator](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/Iterator.html), [LazyList](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/immutable/LazyList.html), [Vector](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/immutable/Vector.html), [StringBuilder](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/mutable/StringBuilder.html), and [Range](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/immutable/Range.html).
@@ -116,27 +128,45 @@ Legend:
116128

117129
The most important collection classes are shown in the figures above. There is quite a bit of commonality shared by all these classes. For instance, every kind of collection can be created by the same uniform syntax, writing the collection class name followed by its elements:
118130

119-
Iterable("x", "y", "z")
120-
Map("x" -> 24, "y" -> 25, "z" -> 26)
121-
Set(Color.red, Color.green, Color.blue)
122-
SortedSet("hello", "world")
123-
Buffer(x, y, z)
124-
IndexedSeq(1.0, 2.0)
125-
LinearSeq(a, b, c)
131+
{% tabs overview_3 %}
132+
{% tab 'Scala 2 and 3' for=overview_3 %}
133+
```scala
134+
Iterable("x", "y", "z")
135+
Map("x" -> 24, "y" -> 25, "z" -> 26)
136+
Set(Color.red, Color.green, Color.blue)
137+
SortedSet("hello", "world")
138+
Buffer(x, y, z)
139+
IndexedSeq(1.0, 2.0)
140+
LinearSeq(a, b, c)
141+
```
142+
{% endtab %}
143+
{% endtabs %}
126144

127145
The same principle also applies for specific collection implementations, such as:
128146

129-
List(1, 2, 3)
130-
HashMap("x" -> 24, "y" -> 25, "z" -> 26)
147+
{% tabs overview_4 %}
148+
{% tab 'Scala 2 and 3' for=overview_4 %}
149+
```scala
150+
List(1, 2, 3)
151+
HashMap("x" -> 24, "y" -> 25, "z" -> 26)
152+
```
153+
{% endtab %}
154+
{% endtabs %}
131155

132156
All these collections get displayed with `toString` in the same way they are written above.
133157

134158
All collections support the API provided by `Iterable`, but specialize types wherever this makes sense. For instance the `map` method in class `Iterable` returns another `Iterable` as its result. But this result type is overridden in subclasses. For instance, calling `map` on a `List` yields again a `List`, calling it on a `Set` yields again a `Set` and so on.
135159

136-
scala> List(1, 2, 3) map (_ + 1)
137-
res0: List[Int] = List(2, 3, 4)
138-
scala> Set(1, 2, 3) map (_ * 2)
139-
res0: Set[Int] = Set(2, 4, 6)
160+
{% tabs overview_5 %}
161+
{% tab 'Scala 2 and 3' for=overview_5 %}
162+
```
163+
scala> List(1, 2, 3) map (_ + 1)
164+
res0: List[Int] = List(2, 3, 4)
165+
scala> Set(1, 2, 3) map (_ * 2)
166+
res0: Set[Int] = Set(2, 4, 6)
167+
```
168+
{% endtab %}
169+
{% endtabs %}
140170

141171
This behavior which is implemented everywhere in the collections libraries is called the _uniform return type principle_.
142172

_overviews/collections-2.13/seqs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The [Seq](https://www.scala-lang.org/api/current/scala/collection/Seq.html) trai
1616

1717
The operations on sequences, summarized in the table below, fall into the following categories:
1818

19-
* **Indexing and length** operations `apply`, `isDefinedAt`, `length`, `indices`, and `lengthCompare`. For a `Seq`, the `apply` operation means indexing; hence a sequence of type `Seq[T]` is a partial function that takes an `Int` argument (an index) and which yields a sequence element of type `T`. In other words `Seq[T]` extends `PartialFunction[Int, T]`. The elements of a sequence are indexed from zero up to the `length` of the sequence minus one. The `length` method on sequences is an alias of the `size` method of general collections. The `lengthCompare` method allows you to compare the lengths of a sequences with an Int even if the sequences has infinite length.
19+
* **Indexing and length** operations `apply`, `isDefinedAt`, `length`, `indices`, and `lengthCompare`. For a `Seq`, the `apply` operation means indexing; hence a sequence of type `Seq[T]` is a partial function that takes an `Int` argument (an index) and which yields a sequence element of type `T`. In other words `Seq[T]` extends `PartialFunction[Int, T]`. The elements of a sequence are indexed from zero up to the `length` of the sequence minus one. The `length` method on sequences is an alias of the `size` method of general collections. The `lengthCompare` method allows you to compare the lengths of a sequences with an Int or with an `Iterable` even if the sequences has infinite length.
2020
* **Index search operations** `indexOf`, `lastIndexOf`, `indexOfSlice`, `lastIndexOfSlice`, `indexWhere`, `lastIndexWhere`, `segmentLength`, which return the index of an element equal to a given value or matching some predicate.
2121
* **Addition operations** `prepended`, `prependedAll`, `appended`, `appendedAll`, `padTo`, which return new sequences obtained by adding elements at the front or the end of a sequence.
2222
* **Update operations** `updated`, `patch`, which return a new sequence obtained by replacing some elements of the original sequence.

_overviews/collections-2.13/trait-iterable.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ permalink: /overviews/collections-2.13/:title.html
1414

1515
At the top of the collection hierarchy is trait `Iterable`. All methods in this trait are defined in terms of an abstract method, `iterator`, which yields the collection's elements one by one.
1616

17-
def iterator: Iterator[A]
17+
{% tabs trait-iterable_1 %}
18+
{% tab 'Scala 2 and 3' for=trait-iterable_1 %}
19+
```scala
20+
def iterator: Iterator[A]
21+
```
22+
{% endtab %}
23+
{% endtabs %}
1824

1925
Collection classes that implement `Iterable` just need to define this method; all other methods can be inherited from `Iterable`.
2026

@@ -31,27 +37,33 @@ Collection classes that implement `Iterable` just need to define this method; al
3137
* **Element tests** `exists`, `forall`, `count` which test collection elements with a given predicate.
3238
* **Folds** `foldLeft`, `foldRight`, `reduceLeft`, `reduceRight` which apply a binary operation to successive elements.
3339
* **Specific folds** `sum`, `product`, `min`, `max`, which work on collections of specific types (numeric or comparable).
34-
* **String** operations `mkString`, `addString`, `className`, which give alternative ways of converting a collection to a string.
40+
* **String** operations `mkString`, `addString`, and the protected `className`, which give alternative ways of converting a collection to a string.
3541
* **View** operation: A view is a collection that's evaluated lazily. You'll learn more about views in [later]({% link _overviews/collections-2.13/views.md %}).
3642

3743
Two more methods exist in `Iterable` that return iterators: `grouped` and `sliding`. These iterators, however, do not return single elements but whole subsequences of elements of the original collection. The maximal size of these subsequences is given as an argument to these methods. The `grouped` method returns its elements in "chunked" increments, where `sliding` yields a sliding "window" over the elements. The difference between the two should become clear by looking at the following REPL interaction:
3844

39-
scala> val xs = List(1, 2, 3, 4, 5)
40-
xs: List[Int] = List(1, 2, 3, 4, 5)
41-
scala> val git = xs grouped 3
42-
git: Iterator[List[Int]] = non-empty iterator
43-
scala> git.next()
44-
res3: List[Int] = List(1, 2, 3)
45-
scala> git.next()
46-
res4: List[Int] = List(4, 5)
47-
scala> val sit = xs sliding 3
48-
sit: Iterator[List[Int]] = non-empty iterator
49-
scala> sit.next()
50-
res5: List[Int] = List(1, 2, 3)
51-
scala> sit.next()
52-
res6: List[Int] = List(2, 3, 4)
53-
scala> sit.next()
54-
res7: List[Int] = List(3, 4, 5)
45+
{% tabs trait-iterable_2 %}
46+
{% tab 'Scala 2 and 3' for=trait-iterable_2 %}
47+
```
48+
scala> val xs = List(1, 2, 3, 4, 5)
49+
xs: List[Int] = List(1, 2, 3, 4, 5)
50+
scala> val git = xs grouped 3
51+
git: Iterator[List[Int]] = non-empty iterator
52+
scala> git.next()
53+
res3: List[Int] = List(1, 2, 3)
54+
scala> git.next()
55+
res4: List[Int] = List(4, 5)
56+
scala> val sit = xs sliding 3
57+
sit: Iterator[List[Int]] = non-empty iterator
58+
scala> sit.next()
59+
res5: List[Int] = List(1, 2, 3)
60+
scala> sit.next()
61+
res6: List[Int] = List(2, 3, 4)
62+
scala> sit.next()
63+
res7: List[Int] = List(3, 4, 5)
64+
```
65+
{% endtab %}
66+
{% endtabs %}
5567

5668
### Operations in Class Iterable ###
5769

0 commit comments

Comments
 (0)