Skip to content

Commit 6968d4b

Browse files
authored
Update higher-order-functions.md
1 parent 6d0098b commit 6968d4b

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

_tour/higher-order-functions.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ val salaries = Seq(20000, 70000, 40000)
2929
val doubleSalary = (x: Int) => x * 2
3030
val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000)
3131
```
32-
{% end tab %}
32+
{% endtab %}
3333

3434
{% tab 'Scala 3' for=map_example_1 %}
3535
```scala
3636
val salaries = Seq(20_000, 70_000, 40_000)
3737
val doubleSalary = (x: Int) => x * 2
3838
val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000)
3939
```
40-
{% end tab %}
40+
{% endtab %}
4141

42-
{% end tabs %}
42+
{% endtabs %}
4343

4444
`doubleSalary` is a function which takes a single Int, `x`, and returns `x * 2`. In general, the tuple on the left of the arrow `=>` is a parameter list and the value of the expression on the right is what gets returned. On line 3, the function `doubleSalary` gets applied to each element in the
4545
list of salaries.
@@ -54,16 +54,16 @@ an argument to map:
5454
val salaries = Seq(20000, 70000, 40000)
5555
val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000)
5656
```
57-
{% end tab %}
57+
{% endtab %}
5858

5959
{% tab 'Scala 3' for=map_example_2 %}
6060
```scala
6161
val salaries = Seq(20_000, 70_000, 40_000)
6262
val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000)
6363
```
64-
{% end tab %}
64+
{% endtab %}
6565

66-
{% end tabs %}
66+
{% endtabs %}
6767

6868
Notice how `x` is not declared as an Int in the above example. That's because the
6969
compiler can infer the type based on the type of function map expects (see [Currying](/tour/multiple-parameter-lists.html)). An even more idiomatic way to write the same piece of code would be:
@@ -75,16 +75,16 @@ compiler can infer the type based on the type of function map expects (see [Curr
7575
val salaries = Seq(20000, 70000, 40000)
7676
val newSalaries = salaries.map(_ * 2)
7777
```
78-
{% end tab %}
78+
{% endtab %}
7979

8080
{% tab 'Scala 3' for=map_example_3 %}
8181
```scala
8282
val salaries = Seq(20_000, 70_000, 40_000)
8383
val newSalaries = salaries.map(_ * 2)
8484
```
85-
{% end tab %}
85+
{% endtab %}
8686

87-
{% end tabs %}
87+
{% endtabs %}
8888

8989
Since the Scala compiler already knows the type of the parameters (a single Int),
9090
you just need to provide the right side of the function. The only
@@ -106,7 +106,7 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) {
106106
def forecastInFahrenheit: Seq[Double] = temperatures.map(convertCtoF) // <-- passing the method convertCtoF
107107
}
108108
```
109-
{% end tab %}
109+
{% endtab %}
110110

111111
{% tab 'Scala 3' for=Coercing_methods_into_functions %}
112112
```scala
@@ -116,9 +116,9 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]):
116116

117117
def forecastInFahrenheit: Seq[Double] = temperatures.map(convertCtoF) // <-- passing the method convertCtoF
118118
```
119-
{% end tab %}
119+
{% endtab %}
120120

121-
{% end tabs %}
121+
{% endtabs %}
122122

123123
Here the method `convertCtoF` is passed to the higher order function `map`. This is possible because the compiler coerces `convertCtoF` to the function `x => convertCtoF(x)` (note: `x` will
124124
be a generated name which is guaranteed to be unique within its scope).
@@ -143,7 +143,7 @@ object SalaryRaiser {
143143
salaries.map(salary => salary * salary)
144144
}
145145
```
146-
{% end tab %}
146+
{% endtab %}
147147

148148
{% tab 'Scala 3' for=Functions_that_accept_functions_1 %}
149149
```scala
@@ -158,9 +158,9 @@ object SalaryRaiser:
158158
def hugePromotion(salaries: List[Double]): List[Double] =
159159
salaries.map(salary => salary * salary)
160160
```
161-
{% end tab %}
161+
{% endtab %}
162162

163-
{% end tabs %}
163+
{% endtabs %}
164164

165165
Notice how each of the three methods vary only by the multiplication factor. To simplify,
166166
you can extract the repeated code into a higher-order function like so:
@@ -184,7 +184,7 @@ object SalaryRaiser {
184184
promotion(salaries, salary => salary * salary)
185185
}
186186
```
187-
{% end tab %}
187+
{% endtab %}
188188

189189
{% tab 'Scala 3' for=Functions_that_accept_functions_2 %}
190190
```scala
@@ -202,9 +202,9 @@ object SalaryRaiser:
202202
def hugePromotion(salaries: List[Double]): List[Double] =
203203
promotion(salaries, salary => salary * salary)
204204
```
205-
{% end tab %}
205+
{% endtab %}
206206

207-
{% end tabs %}
207+
{% endtabs %}
208208

209209
The new method, `promotion`, takes the salaries plus a function of type `Double => Double`
210210
(i.e. a function that takes a Double and returns a Double) and returns the product.
@@ -231,7 +231,7 @@ val endpoint = "users"
231231
val query = "id=1"
232232
val url = getURL(endpoint, query) // "https://www.example.com/users?id=1": String
233233
```
234-
{% end tab %}
234+
{% endtab %}
235235

236236
{% tab 'Scala 3' for=Functions_that_return_functions %}
237237
```scala
@@ -245,9 +245,9 @@ val endpoint = "users"
245245
val query = "id=1"
246246
val url = getURL(endpoint, query) // "https://www.example.com/users?id=1": String
247247
```
248-
{% end tab %}
248+
{% endtab %}
249249

250-
{% end tabs %}
250+
{% endtabs %}
251251

252252
Notice the return type of urlBuilder `(String, String) => String`. This means that
253253
the returned anonymous function takes two Strings and returns a String. In this case,

0 commit comments

Comments
 (0)