@@ -12,77 +12,119 @@ next-page: fun-eta-expansion
12
12
13
13
Going back to this example from the previous section:
14
14
15
+ {% tabs fun-function-variables-1 %}
16
+ {% tab 'Scala 2 and 3' %}
15
17
``` scala
16
18
val doubledInts = ints.map((i : Int ) => i * 2 )
17
19
```
20
+ {% endtab %}
21
+ {% endtabs %}
18
22
19
23
We noted that this part of the expression is an anonymous function:
20
24
25
+ {% tabs fun-function-variables-2 %}
26
+ {% tab 'Scala 2 and 3' %}
21
27
``` scala
22
28
(i : Int ) => i * 2
23
29
```
30
+ {% endtab %}
31
+ {% endtabs %}
24
32
25
33
The reason it’s called * anonymous* is because it’s not assigned to a variable, and therefore doesn’t have a name.
26
34
27
35
However, an anonymous function---also known as a * function literal* ---can be assigned to a variable to create a * function variable* :
28
36
37
+ {% tabs fun-function-variables-3 %}
38
+ {% tab 'Scala 2 and 3' %}
29
39
``` scala
30
40
val double = (i : Int ) => i * 2
31
41
```
42
+ {% endtab %}
43
+ {% endtabs %}
32
44
33
45
This creates a function variable named ` double ` .
34
46
In this expression, the original function literal is on the right side of the ` = ` symbol:
35
47
48
+ {% tabs fun-function-variables-4 %}
49
+ {% tab 'Scala 2 and 3' %}
36
50
``` scala
37
51
val double = (i : Int ) => i * 2
38
52
-----------------
39
53
```
54
+ {% endtab %}
55
+ {% endtabs %}
40
56
41
57
the new variable name is on the left side:
42
58
59
+ {% tabs fun-function-variables-5 %}
60
+ {% tab 'Scala 2 and 3' %}
43
61
``` scala
44
62
val double = (i : Int ) => i * 2
45
63
------
46
64
```
65
+ {% endtab %}
66
+ {% endtabs %}
47
67
48
68
and the function’s parameter list is underlined here:
49
69
70
+ {% tabs fun-function-variables-6 %}
71
+ {% tab 'Scala 2 and 3' %}
50
72
``` scala
51
73
val double = (i : Int ) => i * 2
52
74
--------
53
75
```
76
+ {% endtab %}
77
+ {% endtabs %}
54
78
55
79
Like the parameter list for a method, this means that the ` double ` function takes one parameter, an ` Int ` named ` i ` .
56
80
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 ` :
57
81
82
+ {% tabs fun-function-variables-7 %}
83
+ {% tab 'Scala 2 and 3' %}
58
84
``` scala
59
85
scala> val double = (i : Int ) => i * 2
60
86
val double : Int => Int = ...
61
87
```
88
+ {% endtab %}
89
+ {% endtabs %}
62
90
63
91
64
92
### Invoking the function
65
93
66
94
Now you can call the ` double ` function like this:
67
95
96
+ {% tabs fun-function-variables-8 %}
97
+ {% tab 'Scala 2 and 3' %}
68
98
``` scala
69
99
val x = double(2 ) // 4
70
100
```
101
+ {% endtab %}
102
+ {% endtabs %}
71
103
72
104
You can also pass ` double ` into a ` map ` call:
73
105
106
+ {% tabs fun-function-variables-9 %}
107
+ {% tab 'Scala 2 and 3' %}
74
108
``` scala
75
109
List (1 , 2 , 3 ).map(double) // List(2, 4, 6)
76
110
```
111
+ {% endtab %}
112
+ {% endtabs %}
77
113
78
114
Furthermore, when you have other functions of the ` Int => Int ` type:
79
115
116
+ {% tabs fun-function-variables-10 %}
117
+ {% tab 'Scala 2 and 3' %}
80
118
``` scala
81
119
val triple = (i : Int ) => i * 3
82
120
```
121
+ {% endtab %}
122
+ {% endtabs %}
83
123
84
124
you can store them in a ` List ` or ` Map ` :
85
125
126
+ {% tabs fun-function-variables-11 %}
127
+ {% tab 'Scala 2 and 3' %}
86
128
``` scala
87
129
val functionList = List (double, triple)
88
130
@@ -91,9 +133,13 @@ val functionMap = Map(
91
133
" 3x" -> triple
92
134
)
93
135
```
136
+ {% endtab %}
137
+ {% endtabs %}
94
138
95
139
If you paste those expressions into the REPL, you’ll see that they have these types:
96
140
141
+ {% tabs fun-function-variables-12 %}
142
+ {% tab 'Scala 2 and 3' %}
97
143
````
98
144
// a List that contains functions of the type `Int => Int`
99
145
functionList: List[Int => Int]
@@ -102,6 +148,8 @@ functionList: List[Int => Int]
102
148
// values have the type `Int => Int`
103
149
functionMap: Map[String, Int => Int]
104
150
````
151
+ {% endtab %}
152
+ {% endtabs %}
105
153
106
154
107
155
0 commit comments