Skip to content

Commit a43124f

Browse files
committed
add code tabs in num13.
1 parent 69a362b commit a43124f

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

_overviews/scala3-book/taste-collections.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ next-page: taste-contextual-abstractions
99
---
1010

1111

12-
1312
The Scala library has a rich set of collection classes, and those classes have a rich set of methods.
1413
Collections classes are available in both immutable and mutable forms.
1514

16-
17-
1815
## Creating lists
1916

2017
To give you a taste of how these work, here are some examples that use the `List` class, which is an immutable, linked-list class.
2118
These examples show different ways to create a populated `List`:
2219

20+
{% tabs collection_1 class=tabs-scala-version %}
21+
{% tab 'Scala 2 and 3' for=collection_1 %}
2322
```scala
2423
val a = List(1, 2, 3) // a: List[Int] = List(1, 2, 3)
2524

@@ -30,15 +29,17 @@ val e = (1 until 5).toList // e: List[Int] = List(1, 2, 3, 4)
3029
val f = List.range(1, 5) // f: List[Int] = List(1, 2, 3, 4)
3130
val g = List.range(1, 10, 3) // g: List[Int] = List(1, 4, 7)
3231
```
33-
34-
32+
{% endtab %}
33+
{% endtabs %}
3534

3635
## `List` methods
3736

3837
Once you have a populated list, the following examples show some of the methods you can call on it.
3938
Notice that these are all functional methods, meaning that they don’t mutate the collection they’re called on, but instead return a new collection with the updated elements.
4039
The result that’s returned by each expression is shown in the comment on each line:
4140

41+
{% tabs collection_2 class=tabs-scala-version %}
42+
{% tab 'Scala 2 and 3' for=collection_2 %}
4243
```scala
4344
// a sample list
4445
val a = List(10, 20, 30, 40, 10) // List(10, 20, 30, 40, 10)
@@ -60,45 +61,63 @@ val nums = List("one", "two")
6061
nums.map(_.toUpperCase) // List("ONE", "TWO")
6162
nums.flatMap(_.toUpperCase) // List('O', 'N', 'E', 'T', 'W', 'O')
6263
```
64+
{% endtab %}
65+
{% endtabs %}
6366

6467
These examples show how the “foldLeft” and “reduceLeft” methods are used to sum the values in a sequence of integers:
6568

69+
{% tabs collection_3 class=tabs-scala-version %}
70+
{% tab 'Scala 2 and 3' for=collection_3 %}
6671
```scala
6772
val firstTen = (1 to 10).toList // List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
6873

6974
firstTen.reduceLeft(_ + _) // 55
7075
firstTen.foldLeft(100)(_ + _) // 155 (100 is a “seed” value)
7176
```
77+
{% endtab %}
78+
{% endtabs %}
7279

7380
There are many more methods available to Scala collections classes, and they’re demonstrated in the [Collections chapter][collections], and in the [API Documentation][api].
7481

75-
76-
7782
## Tuples
7883

7984
The Scala _tuple_ is a type that lets you easily put a collection of different types in the same container.
8085
For example, given this `Person` case class:
8186

87+
{% tabs collection_4 class=tabs-scala-version %}
88+
{% tab 'Scala 2 and 3' for=collection_4 %}
8289
```scala
8390
case class Person(name: String)
8491
```
92+
{% endtab %}
93+
{% endtabs %}
8594

8695
This is how you create a tuple that contains an `Int`, a `String`, and a custom `Person` value:
8796

97+
{% tabs collection_5 class=tabs-scala-version %}
98+
{% tab 'Scala 2 and 3' for=collection_5 %}
8899
```scala
89100
val t = (11, "eleven", Person("Eleven"))
90101
```
102+
{% endtab %}
103+
{% endtabs %}
91104

92105
Once you have a tuple, you can access its values by binding them to variables, or access them by number:
93106

107+
{% tabs collection_6 class=tabs-scala-version %}
108+
{% tab 'Scala 2 and 3' for=collection_6 %}
94109
```scala
95110
t(0) // 11
96111
t(1) // "eleven"
97112
t(2) // Person("Eleven")
98113
```
114+
{% endtab %}
115+
{% endtabs %}
99116

100117
You can also use this _extractor_ approach to assign the tuple fields to variable names:
101118

119+
{% tabs collection_7 class=tabs-scala-version %}
120+
{% tab 'Scala 2 and 3' for=collection_7 %}
102121
```scala
103122
val (num, str, person) = t
104123

@@ -107,13 +126,12 @@ val (num, str, person) = t
107126
// val str: String = eleven
108127
// val person: Person = Person(Eleven)
109128
```
129+
{% endtab %}
130+
{% endtabs %}
110131

111132
Tuples are nice for those times when you want to put a collection of heterogeneous types in a little collection-like structure.
112133
See the [Reference documentation][reference] for more tuple details.
113134

114-
115-
116-
117135
[collections]: {% link _overviews/scala3-book/collections-intro.md %}
118136
[api]: https://scala-lang.org/api/3.x/
119137
[reference]: {{ site.scala3ref }}/overview.html

0 commit comments

Comments
 (0)