Skip to content

Commit f2f7072

Browse files
authored
Merge pull request #1351 from cclaudiu81/patch-2
Update higher-order-functions.md
2 parents 2debd5c + 7fb22d5 commit f2f7072

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

_tour/higher-order-functions.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The terminology can get a bit confusing at this point, and we use the phrase
1919
"higher order function" for both methods and functions that take functions as parameters
2020
or that return a function.
2121

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+
2224
One of the most common examples is the higher-order
2325
function `map` which is available for collections in Scala.
2426
```tut
@@ -58,7 +60,7 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) {
5860
def forecastInFahrenheit: Seq[Double] = temperatures.map(convertCtoF) // <-- passing the method convertCtoF
5961
}
6062
```
61-
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
6264
be a generated name which is guaranteed to be unique within its scope).
6365

6466
## Functions that accept functions
@@ -102,6 +104,8 @@ object SalaryRaiser {
102104
The new method, `promotion`, takes the salaries plus a function of type `Double => Double`
103105
(i.e. a function that takes a Double and returns a Double) and returns the product.
104106

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+
105109
## Functions that return functions
106110

107111
There are certain cases where you want to generate a function. Here's an example

0 commit comments

Comments
 (0)