Skip to content

Commit 704f64a

Browse files
committed
Add code tabs to fun-write-method-returns-function
1 parent c875c65 commit 704f64a

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

Diff for: _overviews/scala3-book/fun-write-method-returns-function.md

+76
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,72 @@ Once again we start with a problem statement:
2020
Given that statement, you can start building `greet`.
2121
You know it’s going to be a method:
2222

23+
{% tabs fun-write-method-returns-function-1 %}
24+
{% tab 'Scala 2 and 3' %}
2325
```scala
2426
def greet()
2527
```
28+
{% endtab %}
29+
{% endtabs %}
2630

2731
You also know this method will return a function that (a) takes a `String` parameter, and (b) prints that string using `println`.
2832
Therefore that function has the type, `String => Unit`:
2933

34+
{% tabs fun-write-method-returns-function-2 %}
35+
{% tab 'Scala 2 and 3' %}
3036
```scala
3137
def greet(): String => Unit = ???
3238
----------------
3339
```
40+
{% endtab %}
41+
{% endtabs %}
3442

3543
Now you just need a method body.
3644
You know that the method needs to return a function, and that function takes a `String` and prints it.
3745
This anonymous function matches that description:
3846

47+
{% tabs fun-write-method-returns-function-3 %}
48+
{% tab 'Scala 2 and 3' %}
3949
```scala
4050
(name: String) => println(s"Hello, $name")
4151
```
52+
{% endtab %}
53+
{% endtabs %}
4254

4355
Now you just return that function from the method:
4456

57+
{% tabs fun-write-method-returns-function-4 %}
58+
{% tab 'Scala 2 and 3' %}
4559
```scala
4660
// a method that returns a function
4761
def greet(): String => Unit =
4862
(name: String) => println(s"Hello, $name")
4963
```
64+
{% endtab %}
65+
{% endtabs %}
5066

5167
Because this method returns a function, you get the function by calling `greet()`.
5268
This is a good step to do in the REPL because it verifies the type of the new function:
5369

70+
{% tabs fun-write-method-returns-function-5 %}
71+
{% tab 'Scala 2 and 3' %}
5472
````
5573
scala> val greetFunction = greet()
5674
val greetFunction: String => Unit = Lambda....
5775
-----------------------------
5876
````
77+
{% endtab %}
78+
{% endtabs %}
5979

6080
Now you can call `greetFunction`:
6181

82+
{% tabs fun-write-method-returns-function-6 %}
83+
{% tab 'Scala 2 and 3' %}
6284
```scala
6385
greetFunction("Joe") // prints "Hello, Joe"
6486
```
87+
{% endtab %}
88+
{% endtabs %}
6589

6690
Congratulations, you just created a method that returns a function, and then executed that function.
6791

@@ -72,36 +96,52 @@ Congratulations, you just created a method that returns a function, and then exe
7296
Our method would be more useful if you could pass in a greeting, so let’s do that.
7397
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`:
7498

99+
{% tabs fun-write-method-returns-function-7 %}
100+
{% tab 'Scala 2 and 3' %}
75101
```scala
76102
def greet(theGreeting: String): String => Unit =
77103
(name: String) => println(s"$theGreeting, $name")
78104
```
105+
{% endtab %}
106+
{% endtabs %}
79107

80108
Now when you call your method, the process is more flexible because you can change the greeting.
81109
This is what it looks like when you create a function from this method:
82110

111+
{% tabs fun-write-method-returns-function-8 %}
112+
{% tab 'Scala 2 and 3' %}
83113
````
84114
scala> val sayHello = greet("Hello")
85115
val sayHello: String => Unit = Lambda.....
86116
------------------------
87117
````
118+
{% endtab %}
119+
{% endtabs %}
88120

89121
The REPL type signature output shows that `sayHello` is a function that takes a `String` input parameter and returns `Unit` (nothing).
90122
So now when you give `sayHello` a `String`, it prints the greeting:
91123

124+
{% tabs fun-write-method-returns-function-9 %}
125+
{% tab 'Scala 2 and 3' %}
92126
```scala
93127
sayHello("Joe") // prints "Hello, Joe"
94128
```
129+
{% endtab %}
130+
{% endtabs %}
95131

96132
You can also change the greeting to create new functions, as desired:
97133

134+
{% tabs fun-write-method-returns-function-10 %}
135+
{% tab 'Scala 2 and 3' %}
98136
```scala
99137
val sayCiao = greet("Ciao")
100138
val sayHola = greet("Hola")
101139

102140
sayCiao("Isabella") // prints "Ciao, Isabella"
103141
sayHola("Carlos") // prints "Hola, Carlos"
104142
```
143+
{% endtab %}
144+
{% endtabs %}
105145

106146

107147

@@ -116,27 +156,53 @@ A first thing you know is that you want to create a method that (a) takes a “d
116156
Furthermore, because that function prints a string that it’s given, you know it has the type `String => Unit`.
117157
With that information you write the method signature:
118158

159+
{% tabs fun-write-method-returns-function-11 %}
160+
{% tab 'Scala 2 and 3' %}
119161
```scala
120162
def createGreetingFunction(desiredLanguage: String): String => Unit = ???
121163
```
164+
{% endtab %}
165+
{% endtabs %}
122166

123167
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:
124168

169+
{% tabs fun-write-method-returns-function-12 %}
170+
{% tab 'Scala 2 and 3' %}
125171
```scala
126172
(name: String) => println(s"Hello, $name")
127173
(name: String) => println(s"Bonjour, $name")
128174
```
175+
{% endtab %}
176+
{% endtabs %}
129177

130178
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:
131179

180+
{% tabs fun-write-method-returns-function-13 %}
181+
{% tab 'Scala 2 and 3' %}
132182
```scala
133183
val englishGreeting = (name: String) => println(s"Hello, $name")
134184
val frenchGreeting = (name: String) => println(s"Bonjour, $name")
135185
```
186+
{% endtab %}
187+
{% endtabs %}
136188

137189
Now all you need to do is (a) return `englishGreeting` if the `desiredLanguage` is English, and (b) return `frenchGreeting` if the `desiredLanguage` is French.
138190
One way to do that is with a `match` expression:
139191

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 %}
140206
```scala
141207
def createGreetingFunction(desiredLanguage: String): String => Unit =
142208
val englishGreeting = (name: String) => println(s"Hello, $name")
@@ -145,23 +211,33 @@ def createGreetingFunction(desiredLanguage: String): String => Unit =
145211
case "english" => englishGreeting
146212
case "french" => frenchGreeting
147213
```
214+
{% endtab %}
215+
{% endtabs %}
148216

149217
And that’s the final method.
150218
Notice that returning a function value from a method is no different than returning a string or integer value.
151219

152220
This is how `createGreetingFunction` builds a French-greeting function:
153221

222+
{% tabs fun-write-method-returns-function-16 %}
223+
{% tab 'Scala 2 and 3' %}
154224
```scala
155225
val greetInFrench = createGreetingFunction("french")
156226
greetInFrench("Jonathan") // prints "Bonjour, Jonathan"
157227
```
228+
{% endtab %}
229+
{% endtabs %}
158230

159231
And this is how it builds an English-greeting function:
160232

233+
{% tabs fun-write-method-returns-function-17 %}
234+
{% tab 'Scala 2 and 3' %}
161235
```scala
162236
val greetInEnglish = createGreetingFunction("english")
163237
greetInEnglish("Joe") // prints "Hello, Joe"
164238
```
239+
{% endtab %}
240+
{% endtabs %}
165241

166242
If you’re comfortable with that code---congratulations---you now know how to write methods that return functions.
167243

0 commit comments

Comments
 (0)