You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`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
45
45
list of salaries.
@@ -54,16 +54,16 @@ an argument to map:
54
54
val salaries = Seq(20000, 70000, 40000)
55
55
val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000)
56
56
```
57
-
{% end tab %}
57
+
{% endtab %}
58
58
59
59
{% tab 'Scala 3' for=map_example_2 %}
60
60
```scala
61
61
valsalaries=Seq(20_000, 70_000, 40_000)
62
62
valnewSalaries= salaries.map(x => x *2) // List(40000, 140000, 80000)
63
63
```
64
-
{% end tab %}
64
+
{% endtab %}
65
65
66
-
{% end tabs %}
66
+
{% endtabs %}
67
67
68
68
Notice how `x` is not declared as an Int in the above example. That's because the
69
69
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
75
75
valsalaries=Seq(20000, 70000, 40000)
76
76
valnewSalaries= salaries.map(_ *2)
77
77
```
78
-
{% end tab %}
78
+
{% endtab %}
79
79
80
80
{% tab 'Scala 3' for=map_example_3 %}
81
81
```scala
82
82
valsalaries=Seq(20_000, 70_000, 40_000)
83
83
valnewSalaries= salaries.map(_ *2)
84
84
```
85
-
{% end tab %}
85
+
{% endtab %}
86
86
87
-
{% end tabs %}
87
+
{% endtabs %}
88
88
89
89
Since the Scala compiler already knows the type of the parameters (a single Int),
90
90
you just need to provide the right side of the function. The only
@@ -106,7 +106,7 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) {
106
106
defforecastInFahrenheit:Seq[Double] = temperatures.map(convertCtoF) // <-- passing the method convertCtoF
@@ -116,9 +116,9 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]):
116
116
117
117
defforecastInFahrenheit:Seq[Double] = temperatures.map(convertCtoF) // <-- passing the method convertCtoF
118
118
```
119
-
{% end tab %}
119
+
{% endtab %}
120
120
121
-
{% end tabs %}
121
+
{% endtabs %}
122
122
123
123
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
124
124
be a generated name which is guaranteed to be unique within its scope).
0 commit comments