@@ -20,48 +20,72 @@ Once again we start with a problem statement:
20
20
Given that statement, you can start building ` greet ` .
21
21
You know it’s going to be a method:
22
22
23
+ {% tabs fun-write-method-returns-function-1 %}
24
+ {% tab 'Scala 2 and 3' %}
23
25
``` scala
24
26
def greet ()
25
27
```
28
+ {% endtab %}
29
+ {% endtabs %}
26
30
27
31
You also know this method will return a function that (a) takes a ` String ` parameter, and (b) prints that string using ` println ` .
28
32
Therefore that function has the type, ` String => Unit ` :
29
33
34
+ {% tabs fun-write-method-returns-function-2 %}
35
+ {% tab 'Scala 2 and 3' %}
30
36
``` scala
31
37
def greet (): String => Unit = ???
32
38
----------------
33
39
```
40
+ {% endtab %}
41
+ {% endtabs %}
34
42
35
43
Now you just need a method body.
36
44
You know that the method needs to return a function, and that function takes a ` String ` and prints it.
37
45
This anonymous function matches that description:
38
46
47
+ {% tabs fun-write-method-returns-function-3 %}
48
+ {% tab 'Scala 2 and 3' %}
39
49
``` scala
40
50
(name : String ) => println(s " Hello, $name" )
41
51
```
52
+ {% endtab %}
53
+ {% endtabs %}
42
54
43
55
Now you just return that function from the method:
44
56
57
+ {% tabs fun-write-method-returns-function-4 %}
58
+ {% tab 'Scala 2 and 3' %}
45
59
``` scala
46
60
// a method that returns a function
47
61
def greet (): String => Unit =
48
62
(name : String ) => println(s " Hello, $name" )
49
63
```
64
+ {% endtab %}
65
+ {% endtabs %}
50
66
51
67
Because this method returns a function, you get the function by calling ` greet() ` .
52
68
This is a good step to do in the REPL because it verifies the type of the new function:
53
69
70
+ {% tabs fun-write-method-returns-function-5 %}
71
+ {% tab 'Scala 2 and 3' %}
54
72
````
55
73
scala> val greetFunction = greet()
56
74
val greetFunction: String => Unit = Lambda....
57
75
-----------------------------
58
76
````
77
+ {% endtab %}
78
+ {% endtabs %}
59
79
60
80
Now you can call ` greetFunction ` :
61
81
82
+ {% tabs fun-write-method-returns-function-6 %}
83
+ {% tab 'Scala 2 and 3' %}
62
84
``` scala
63
85
greetFunction(" Joe" ) // prints "Hello, Joe"
64
86
```
87
+ {% endtab %}
88
+ {% endtabs %}
65
89
66
90
Congratulations, you just created a method that returns a function, and then executed that function.
67
91
@@ -72,36 +96,52 @@ Congratulations, you just created a method that returns a function, and then exe
72
96
Our method would be more useful if you could pass in a greeting, so let’s do that.
73
97
All you have to do is pass the greeting in as a parameter to the ` greet ` method, and use it in the string inside ` println ` :
74
98
99
+ {% tabs fun-write-method-returns-function-7 %}
100
+ {% tab 'Scala 2 and 3' %}
75
101
``` scala
76
102
def greet (theGreeting : String ): String => Unit =
77
103
(name : String ) => println(s " $theGreeting, $name" )
78
104
```
105
+ {% endtab %}
106
+ {% endtabs %}
79
107
80
108
Now when you call your method, the process is more flexible because you can change the greeting.
81
109
This is what it looks like when you create a function from this method:
82
110
111
+ {% tabs fun-write-method-returns-function-8 %}
112
+ {% tab 'Scala 2 and 3' %}
83
113
````
84
114
scala> val sayHello = greet("Hello")
85
115
val sayHello: String => Unit = Lambda.....
86
116
------------------------
87
117
````
118
+ {% endtab %}
119
+ {% endtabs %}
88
120
89
121
The REPL type signature output shows that ` sayHello ` is a function that takes a ` String ` input parameter and returns ` Unit ` (nothing).
90
122
So now when you give ` sayHello ` a ` String ` , it prints the greeting:
91
123
124
+ {% tabs fun-write-method-returns-function-9 %}
125
+ {% tab 'Scala 2 and 3' %}
92
126
``` scala
93
127
sayHello(" Joe" ) // prints "Hello, Joe"
94
128
```
129
+ {% endtab %}
130
+ {% endtabs %}
95
131
96
132
You can also change the greeting to create new functions, as desired:
97
133
134
+ {% tabs fun-write-method-returns-function-10 %}
135
+ {% tab 'Scala 2 and 3' %}
98
136
``` scala
99
137
val sayCiao = greet(" Ciao" )
100
138
val sayHola = greet(" Hola" )
101
139
102
140
sayCiao(" Isabella" ) // prints "Ciao, Isabella"
103
141
sayHola(" Carlos" ) // prints "Hola, Carlos"
104
142
```
143
+ {% endtab %}
144
+ {% endtabs %}
105
145
106
146
107
147
@@ -116,27 +156,53 @@ A first thing you know is that you want to create a method that (a) takes a “d
116
156
Furthermore, because that function prints a string that it’s given, you know it has the type ` String => Unit ` .
117
157
With that information you write the method signature:
118
158
159
+ {% tabs fun-write-method-returns-function-11 %}
160
+ {% tab 'Scala 2 and 3' %}
119
161
``` scala
120
162
def createGreetingFunction (desiredLanguage : String ): String => Unit = ???
121
163
```
164
+ {% endtab %}
165
+ {% endtabs %}
122
166
123
167
Next, because you know that the possible functions you’ll return take a string and print it, you can write two anonymous functions for the English and French languages:
124
168
169
+ {% tabs fun-write-method-returns-function-12 %}
170
+ {% tab 'Scala 2 and 3' %}
125
171
``` scala
126
172
(name : String ) => println(s " Hello, $name" )
127
173
(name : String ) => println(s " Bonjour, $name" )
128
174
```
175
+ {% endtab %}
176
+ {% endtabs %}
129
177
130
178
Inside a method it might be a little more readable if you give those anonymous functions some names, so let’s assign them to two variables:
131
179
180
+ {% tabs fun-write-method-returns-function-13 %}
181
+ {% tab 'Scala 2 and 3' %}
132
182
``` scala
133
183
val englishGreeting = (name : String ) => println(s " Hello, $name" )
134
184
val frenchGreeting = (name : String ) => println(s " Bonjour, $name" )
135
185
```
186
+ {% endtab %}
187
+ {% endtabs %}
136
188
137
189
Now all you need to do is (a) return ` englishGreeting ` if the ` desiredLanguage ` is English, and (b) return ` frenchGreeting ` if the ` desiredLanguage ` is French.
138
190
One way to do that is with a ` match ` expression:
139
191
192
+ {% tabs fun-write-method-returns-function-14 class=tabs-scala-version %}
193
+ {% tab 'Scala 2' for=fun-write-method-returns-function-14 %}
194
+ ``` scala
195
+ def createGreetingFunction (desiredLanguage : String ): String => Unit = {
196
+ val englishGreeting = (name : String ) => println(s " Hello, $name" )
197
+ val frenchGreeting = (name : String ) => println(s " Bonjour, $name" )
198
+ desiredLanguage match {
199
+ case " english" => englishGreeting
200
+ case " french" => frenchGreeting
201
+ }
202
+ }
203
+ ```
204
+ {% endtab %}
205
+ {% tab 'Scala 3' for=fun-write-method-returns-function-14 %}
140
206
``` scala
141
207
def createGreetingFunction (desiredLanguage : String ): String => Unit =
142
208
val englishGreeting = (name : String ) => println(s " Hello, $name" )
@@ -145,23 +211,33 @@ def createGreetingFunction(desiredLanguage: String): String => Unit =
145
211
case " english" => englishGreeting
146
212
case " french" => frenchGreeting
147
213
```
214
+ {% endtab %}
215
+ {% endtabs %}
148
216
149
217
And that’s the final method.
150
218
Notice that returning a function value from a method is no different than returning a string or integer value.
151
219
152
220
This is how ` createGreetingFunction ` builds a French-greeting function:
153
221
222
+ {% tabs fun-write-method-returns-function-15 %}
223
+ {% tab 'Scala 2 and 3' %}
154
224
``` scala
155
225
val greetInFrench = createGreetingFunction(" french" )
156
226
greetInFrench(" Jonathan" ) // prints "Bonjour, Jonathan"
157
227
```
228
+ {% endtab %}
229
+ {% endtabs %}
158
230
159
231
And this is how it builds an English-greeting function:
160
232
233
+ {% tabs fun-write-method-returns-function-16 %}
234
+ {% tab 'Scala 2 and 3' %}
161
235
``` scala
162
236
val greetInEnglish = createGreetingFunction(" english" )
163
237
greetInEnglish(" Joe" ) // prints "Hello, Joe"
164
238
```
239
+ {% endtab %}
240
+ {% endtabs %}
165
241
166
242
If you’re comfortable with that code---congratulations---you now know how to write methods that return functions.
167
243
0 commit comments