Skip to content

Commit 382a5cc

Browse files
authored
Update higher-order-functions.md
1 parent 8e87cbb commit 382a5cc

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

_tour/higher-order-functions.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ function `map` which is available for collections in Scala.
2323

2424
{% tabs map_example_1 %}
2525

26-
{% tab 'Scala 2 and 3' for=map_example_1 %}
26+
{% tab 'Scala 2' for=map_example_1 %}
2727
```scala mdoc
28+
val salaries = Seq(20000, 70000, 40000)
29+
val doubleSalary = (x: Int) => x * 2
30+
val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000)
31+
```
32+
{% end tab %}
33+
34+
{% tab 'Scala 3' for=map_example_1 %}
35+
```scala
2836
val salaries = Seq(20_000, 70_000, 40_000)
2937
val doubleSalary = (x: Int) => x * 2
3038
val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000)
@@ -41,21 +49,36 @@ an argument to map:
4149

4250
{% tabs map_example_2 %}
4351

44-
{% tab 'Scala 2 and 3' for=map_example_2 %}
52+
{% tab 'Scala 2' for=map_example_2 %}
4553
```scala:nest
54+
val salaries = Seq(20000, 70000, 40000)
55+
val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000)
56+
```
57+
{% end tab %}
58+
59+
{% tab 'Scala 3' for=map_example_2 %}
60+
```scala
4661
val salaries = Seq(20_000, 70_000, 40_000)
4762
val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000)
4863
```
4964
{% end tab %}
5065

5166
{% end tabs %}
67+
5268
Notice how `x` is not declared as an Int in the above example. That's because the
5369
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:
5470

5571
{% tabs map_example_3 %}
5672

57-
{% tab 'Scala 2 and 3' for=map_example_3 %}
73+
{% tab 'Scala 2' for=map_example_3 %}
5874
```scala mdoc:nest
75+
val salaries = Seq(20000, 70000, 40000)
76+
val newSalaries = salaries.map(_ * 2)
77+
```
78+
{% end tab %}
79+
80+
{% tab 'Scala 3' for=map_example_3 %}
81+
```scala mdoc
5982
val salaries = Seq(20_000, 70_000, 40_000)
6083
val newSalaries = salaries.map(_ * 2)
6184
```

0 commit comments

Comments
 (0)