@@ -19,6 +19,7 @@ These examples show different ways to create a populated `List`:
19
19
20
20
{% tabs collection_1 %}
21
21
{% tab 'Scala 2 and 3' for=collection_1 %}
22
+
22
23
``` scala
23
24
val a = List (1 , 2 , 3 ) // a: List[Int] = List(1, 2, 3)
24
25
@@ -29,6 +30,7 @@ val e = (1 until 5).toList // e: List[Int] = List(1, 2, 3, 4)
29
30
val f = List .range(1 , 5 ) // f: List[Int] = List(1, 2, 3, 4)
30
31
val g = List .range(1 , 10 , 3 ) // g: List[Int] = List(1, 4, 7)
31
32
```
33
+
32
34
{% endtab %}
33
35
{% endtabs %}
34
36
@@ -38,8 +40,9 @@ Once you have a populated list, the following examples show some of the methods
38
40
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.
39
41
The result that’s returned by each expression is shown in the comment on each line:
40
42
41
- {% tabs collection_2 class=tabs-scala-version %}
43
+ {% tabs collection_2 %}
42
44
{% tab 'Scala 2 and 3' for=collection_2 %}
45
+
43
46
``` scala
44
47
// a sample list
45
48
val a = List (10 , 20 , 30 , 40 , 10 ) // List(10, 20, 30, 40, 10)
@@ -61,19 +64,22 @@ val nums = List("one", "two")
61
64
nums.map(_.toUpperCase) // List("ONE", "TWO")
62
65
nums.flatMap(_.toUpperCase) // List('O', 'N', 'E', 'T', 'W', 'O')
63
66
```
67
+
64
68
{% endtab %}
65
69
{% endtabs %}
66
70
67
71
These examples show how the “foldLeft” and “reduceLeft” methods are used to sum the values in a sequence of integers:
68
72
69
- {% tabs collection_3 class=tabs-scala-version %}
73
+ {% tabs collection_3 %}
70
74
{% tab 'Scala 2 and 3' for=collection_3 %}
75
+
71
76
``` scala
72
77
val firstTen = (1 to 10 ).toList // List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
73
78
74
79
firstTen.reduceLeft(_ + _) // 55
75
80
firstTen.foldLeft(100 )(_ + _) // 155 (100 is a “seed” value)
76
81
```
82
+
77
83
{% endtab %}
78
84
{% endtabs %}
79
85
@@ -84,40 +90,47 @@ There are many more methods available to Scala collections classes, and they’r
84
90
The Scala _ tuple_ is a type that lets you easily put a collection of different types in the same container.
85
91
For example, given this ` Person ` case class:
86
92
87
- {% tabs collection_4 class=tabs-scala-version %}
93
+ {% tabs collection_4 %}
88
94
{% tab 'Scala 2 and 3' for=collection_4 %}
95
+
89
96
``` scala
90
97
case class Person (name : String )
91
98
```
99
+
92
100
{% endtab %}
93
101
{% endtabs %}
94
102
95
103
This is how you create a tuple that contains an ` Int ` , a ` String ` , and a custom ` Person ` value:
96
104
97
- {% tabs collection_5 class=tabs-scala-version %}
105
+ {% tabs collection_5 %}
98
106
{% tab 'Scala 2 and 3' for=collection_5 %}
107
+
99
108
``` scala
100
109
val t = (11 , " eleven" , Person (" Eleven" ))
101
110
```
111
+
102
112
{% endtab %}
103
113
{% endtabs %}
104
114
105
115
Once you have a tuple, you can access its values by binding them to variables, or access them by number:
106
116
107
- {% tabs collection_6 class=tabs-scala-version %}
117
+ {% tabs collection_6 %}
108
118
{% tab 'Scala 2 and 3' for=collection_6 %}
119
+
109
120
``` scala
110
121
t(0 ) // 11
111
122
t(1 ) // "eleven"
112
123
t(2 ) // Person("Eleven")
113
124
```
125
+
114
126
{% endtab %}
115
127
{% endtabs %}
116
128
117
129
You can also use this _ extractor_ approach to assign the tuple fields to variable names:
118
130
119
- {% tabs collection_7 class=tabs-scala-version %}
131
+ {% tabs collection_7 %}
120
132
{% tab 'Scala 2 and 3' for=collection_7 %}
133
+
121
134
``` scala
122
135
val (num, str, person) = t
123
136
@@ -126,6 +139,7 @@ val (num, str, person) = t
126
139
// val str: String = eleven
127
140
// val person: Person = Person(Eleven)
128
141
```
142
+
129
143
{% endtab %}
130
144
{% endtabs %}
131
145
0 commit comments