You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
17
17
extended in place. This means you can change, add, or remove elements
18
18
of a collection as a side effect. _Immutable_ collections, by
19
19
contrast, never change. You have still operations that simulate
@@ -43,7 +43,7 @@ A collection in package `scala.collection` can be either mutable or
43
43
immutable. For instance, [collection.IndexedSeq\[T\]](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/IndexedSeq.html)
44
44
is a superclass of both [collection.immutable.IndexedSeq\[T\]](https://www.scala-lang.org/api/{{ site.scala-version }}/scala/collection/immutable/IndexedSeq.html)
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:
118
130
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 %}
126
144
127
145
The same principle also applies for specific collection implementations, such as:
128
146
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 %}
131
155
132
156
All these collections get displayed with `toString` in the same way they are written above.
133
157
134
158
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.
135
159
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 %}
140
170
141
171
This behavior which is implemented everywhere in the collections libraries is called the _uniform return type principle_.
Copy file name to clipboardExpand all lines: _overviews/collections-2.13/seqs.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ The [Seq](https://www.scala-lang.org/api/current/scala/collection/Seq.html) trai
16
16
17
17
The operations on sequences, summarized in the table below, fall into the following categories:
18
18
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.
20
20
***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.
21
21
***Addition operations**`prepended`, `prependedAll`, `appended`, `appendedAll`, `padTo`, which return new sequences obtained by adding elements at the front or the end of a sequence.
22
22
***Update operations**`updated`, `patch`, which return a new sequence obtained by replacing some elements of the original sequence.
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.
16
16
17
-
def iterator: Iterator[A]
17
+
{% tabs trait-iterable_1 %}
18
+
{% tab 'Scala 2 and 3' for=trait-iterable_1 %}
19
+
```scala
20
+
defiterator:Iterator[A]
21
+
```
22
+
{% endtab %}
23
+
{% endtabs %}
18
24
19
25
Collection classes that implement `Iterable` just need to define this method; all other methods can be inherited from `Iterable`.
20
26
@@ -31,27 +37,33 @@ Collection classes that implement `Iterable` just need to define this method; al
31
37
***Element tests**`exists`, `forall`, `count` which test collection elements with a given predicate.
32
38
***Folds**`foldLeft`, `foldRight`, `reduceLeft`, `reduceRight` which apply a binary operation to successive elements.
33
39
***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.
35
41
***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 %}).
36
42
37
43
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:
0 commit comments