From 3d34ae738f48edd3c0e85093269867296a0f4c89 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 10 May 2021 13:40:22 +0200 Subject: [PATCH 1/2] Update fp-pure-functions.md The discussion of impurity was technically not correct. `foreach` alone is not impure. Also `const Unit` is a perfectly pure function. So we need to be a bit more precise here in order to not confuse beginners. --- _overviews/scala3-book/fp-pure-functions.md | 22 +++++++++------------ 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/_overviews/scala3-book/fp-pure-functions.md b/_overviews/scala3-book/fp-pure-functions.md index e9aff86437..8c540bad33 100644 --- a/_overviews/scala3-book/fp-pure-functions.md +++ b/_overviews/scala3-book/fp-pure-functions.md @@ -50,24 +50,20 @@ Most methods on the Scala collections classes also work as pure functions, inclu Conversely, the following functions are _impure_ because they violate the definition. -The `foreach` method on collections classes is impure because it’s only used for its side effects, such as printing to STDOUT. +- `println` -- methods that interact with the console, files, databases, web services, sensors, etc., are all impure. +- `getHour` -- date and time related methods are all impure because their output depends on something other than their input parameters +- `sys.error` -- exception throwing methods are impure because they do not simply return a result -> A great hint that `foreach` is impure is that it’s method signature declares that it returns the type `Unit`. -> Because it doesn’t return anything, logically the only reason you ever call it is to achieve some side effect. -> Similarly, _any_ method that returns `Unit` is going to be an impure function. +Impure functions often do one or more of these things: -Date and time related methods like `getDayOfWeek`, `getHour`, and `getMinute` are all impure because their output depends on something other than their input parameters. -Their results rely on some form of hidden I/O; _hidden inputs,_ in these examples. - -Additionally, methods that interact with the console, files, databases, web services, sensors, etc., are all impure. - -In general, impure functions do one or more of these things: - -- Read hidden inputs, i.e., they access variables and data not explicitly passed into the function as input parameters -- Write hidden outputs +- Read from hidden state, i.e., they access variables and data not explicitly passed into the function as input parameters +- Write to hidden state - Mutate the parameters they’re given, or mutate hidden variables, such as fields in their containing class - Perform some sort of I/O with the outside world +> In general, you should watch out for functions with a return type of `Unit`. +> Because those functions do not return anything, logically the only reason you ever call it is to achieve some side effect. +> In consequence, often the usage of those functions is impure. ## But impure functions are needed ... From 7ed0a1cd78aa66295da8bcd6da382bde2c6f187b Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 10 May 2021 17:27:14 +0200 Subject: [PATCH 2/2] Update _overviews/scala3-book/fp-pure-functions.md --- _overviews/scala3-book/fp-pure-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/fp-pure-functions.md b/_overviews/scala3-book/fp-pure-functions.md index 8c540bad33..82199b14e0 100644 --- a/_overviews/scala3-book/fp-pure-functions.md +++ b/_overviews/scala3-book/fp-pure-functions.md @@ -51,7 +51,7 @@ Most methods on the Scala collections classes also work as pure functions, inclu Conversely, the following functions are _impure_ because they violate the definition. - `println` -- methods that interact with the console, files, databases, web services, sensors, etc., are all impure. -- `getHour` -- date and time related methods are all impure because their output depends on something other than their input parameters +- `currentTimeMillis ` -- date and time related methods are all impure because their output depends on something other than their input parameters - `sys.error` -- exception throwing methods are impure because they do not simply return a result Impure functions often do one or more of these things: