From b3bd621d8e8ddda1b726aa8c473186dd996447ed Mon Sep 17 00:00:00 2001 From: cclaudiu81 Date: Fri, 7 Jun 2019 00:30:42 +0300 Subject: [PATCH 1/2] Update higher-order-functions.md enhanced doc on methods that take fns as args --- _tour/higher-order-functions.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/_tour/higher-order-functions.md b/_tour/higher-order-functions.md index efc8d37db2..d6d1681894 100644 --- a/_tour/higher-order-functions.md +++ b/_tour/higher-order-functions.md @@ -60,6 +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 be a generated name which is guaranteed to be unique within its scope). + +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. ## Functions that accept functions 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, From 7fb22d57689d82e20f7bdac4926bbba719d30448 Mon Sep 17 00:00:00 2001 From: cclaudiu81 Date: Fri, 21 Jun 2019 23:11:20 +0300 Subject: [PATCH 2/2] PR changes refactored OO info in the page intro & change small things based on the PR review. --- _tour/higher-order-functions.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/_tour/higher-order-functions.md b/_tour/higher-order-functions.md index d6d1681894..f080bb8350 100644 --- a/_tour/higher-order-functions.md +++ b/_tour/higher-order-functions.md @@ -19,6 +19,8 @@ The terminology can get a bit confusing at this point, and we use the phrase "higher order function" for both methods and functions that take functions as parameters or that return a function. +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. + One of the most common examples is the higher-order function `map` which is available for collections in Scala. ```tut @@ -58,10 +60,8 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) { def forecastInFahrenheit: Seq[Double] = temperatures.map(convertCtoF) // <-- passing the method convertCtoF } ``` -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 +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 be a generated name which is guaranteed to be unique within its scope). - -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. ## Functions that accept functions 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 { The new method, `promotion`, takes the salaries plus a function of type `Double => Double` (i.e. a function that takes a Double and returns a Double) and returns the product. +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. + ## Functions that return functions There are certain cases where you want to generate a function. Here's an example