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
Copy file name to clipboardExpand all lines: _tour/higher-order-functions.md
+5-3
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,8 @@ The terminology can get a bit confusing at this point, and we use the phrase
19
19
"higher order function" for both methods and functions that take functions as parameters
20
20
or that return a function.
21
21
22
+
In a pure Object Oriented world a good practice is to avoid exposing methods parameterized with functions that might leak object's internal state. Leaking internal state might break the invariants of the object itself thus violating encapsulation.
23
+
22
24
One of the most common examples is the higher-order
23
25
function `map` which is available for collections in Scala.
24
26
```tut
@@ -58,10 +60,8 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) {
Here the method `convertCtoF` is passed to `forecastInFahrenheit`. This is possible because the compiler coerces `convertCtoF` to the function `x => convertCtoF(x)` (note: `x` will
63
+
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
62
64
be a generated name which is guaranteed to be unique within its scope).
63
-
64
-
In a pure Object Oriented world a good practice is to avoid exposing methods that are parameterized with functions that might escape the internal state of the objects. Leaking internal state might break the invariants of the object itself.
65
65
66
66
## Functions that accept functions
67
67
One reason to use higher-order functions is to reduce redundant code. Let's say you wanted some methods that could raise someone's salaries by various factors. Without creating a higher-order function,
@@ -104,6 +104,8 @@ object SalaryRaiser {
104
104
The new method, `promotion`, takes the salaries plus a function of type `Double => Double`
105
105
(i.e. a function that takes a Double and returns a Double) and returns the product.
106
106
107
+
Methods and functions usually express behaviours or data transformations, therefore having functions that compose based on other functions can help building generic mechanisms. Those generic operations defer to lock down the entire operation behaviour giving clients a way to control or further customize parts of the operation itself.
108
+
107
109
## Functions that return functions
108
110
109
111
There are certain cases where you want to generate a function. Here's an example
0 commit comments