Skip to content

Commit cbb493e

Browse files
authored
Merge pull request #2623 from HarrisonCheng/code_tabs_fun-function-variables
Add code tabs in fun-function-variables.md
2 parents c4ce985 + e126b1f commit cbb493e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Diff for: _overviews/scala3-book/fun-function-variables.md

+48
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,119 @@ next-page: fun-eta-expansion
1212

1313
Going back to this example from the previous section:
1414

15+
{% tabs fun-function-variables-1 %}
16+
{% tab 'Scala 2 and 3' %}
1517
```scala
1618
val doubledInts = ints.map((i: Int) => i * 2)
1719
```
20+
{% endtab %}
21+
{% endtabs %}
1822

1923
We noted that this part of the expression is an anonymous function:
2024

25+
{% tabs fun-function-variables-2 %}
26+
{% tab 'Scala 2 and 3' %}
2127
```scala
2228
(i: Int) => i * 2
2329
```
30+
{% endtab %}
31+
{% endtabs %}
2432

2533
The reason it’s called *anonymous* is because it’s not assigned to a variable, and therefore doesn’t have a name.
2634

2735
However, an anonymous function---also known as a *function literal*---can be assigned to a variable to create a *function variable*:
2836

37+
{% tabs fun-function-variables-3 %}
38+
{% tab 'Scala 2 and 3' %}
2939
```scala
3040
val double = (i: Int) => i * 2
3141
```
42+
{% endtab %}
43+
{% endtabs %}
3244

3345
This creates a function variable named `double`.
3446
In this expression, the original function literal is on the right side of the `=` symbol:
3547

48+
{% tabs fun-function-variables-4 %}
49+
{% tab 'Scala 2 and 3' %}
3650
```scala
3751
val double = (i: Int) => i * 2
3852
-----------------
3953
```
54+
{% endtab %}
55+
{% endtabs %}
4056

4157
the new variable name is on the left side:
4258

59+
{% tabs fun-function-variables-5 %}
60+
{% tab 'Scala 2 and 3' %}
4361
```scala
4462
val double = (i: Int) => i * 2
4563
------
4664
```
65+
{% endtab %}
66+
{% endtabs %}
4767

4868
and the function’s parameter list is underlined here:
4969

70+
{% tabs fun-function-variables-6 %}
71+
{% tab 'Scala 2 and 3' %}
5072
```scala
5173
val double = (i: Int) => i * 2
5274
--------
5375
```
76+
{% endtab %}
77+
{% endtabs %}
5478

5579
Like the parameter list for a method, this means that the `double` function takes one parameter, an `Int` named `i`.
5680
You can see in the REPL that `double` has the type `Int => Int`, meaning that it takes a single `Int` parameter and returns an `Int`:
5781

82+
{% tabs fun-function-variables-7 %}
83+
{% tab 'Scala 2 and 3' %}
5884
```scala
5985
scala> val double = (i: Int) => i * 2
6086
val double: Int => Int = ...
6187
```
88+
{% endtab %}
89+
{% endtabs %}
6290

6391

6492
### Invoking the function
6593

6694
Now you can call the `double` function like this:
6795

96+
{% tabs fun-function-variables-8 %}
97+
{% tab 'Scala 2 and 3' %}
6898
```scala
6999
val x = double(2) // 4
70100
```
101+
{% endtab %}
102+
{% endtabs %}
71103

72104
You can also pass `double` into a `map` call:
73105

106+
{% tabs fun-function-variables-9 %}
107+
{% tab 'Scala 2 and 3' %}
74108
```scala
75109
List(1, 2, 3).map(double) // List(2, 4, 6)
76110
```
111+
{% endtab %}
112+
{% endtabs %}
77113

78114
Furthermore, when you have other functions of the `Int => Int` type:
79115

116+
{% tabs fun-function-variables-10 %}
117+
{% tab 'Scala 2 and 3' %}
80118
```scala
81119
val triple = (i: Int) => i * 3
82120
```
121+
{% endtab %}
122+
{% endtabs %}
83123

84124
you can store them in a `List` or `Map`:
85125

126+
{% tabs fun-function-variables-11 %}
127+
{% tab 'Scala 2 and 3' %}
86128
```scala
87129
val functionList = List(double, triple)
88130

@@ -91,9 +133,13 @@ val functionMap = Map(
91133
"3x" -> triple
92134
)
93135
```
136+
{% endtab %}
137+
{% endtabs %}
94138

95139
If you paste those expressions into the REPL, you’ll see that they have these types:
96140

141+
{% tabs fun-function-variables-12 %}
142+
{% tab 'Scala 2 and 3' %}
97143
````
98144
// a List that contains functions of the type `Int => Int`
99145
functionList: List[Int => Int]
@@ -102,6 +148,8 @@ functionList: List[Int => Int]
102148
// values have the type `Int => Int`
103149
functionMap: Map[String, Int => Int]
104150
````
151+
{% endtab %}
152+
{% endtabs %}
105153

106154

107155

0 commit comments

Comments
 (0)