-
Notifications
You must be signed in to change notification settings - Fork 1k
clarify wording on pure-functions overview to allow immutable hidden state, and add an example #3134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
clarify wording on pure-functions overview to allow immutable hidden state, and add an example #3134
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ Conversely, the following functions are _impure_ because they violate the defini | |
|
||
Impure functions often do one or more of these things: | ||
|
||
- Read from hidden mutable state, i.e., they access non-constant variables and data not explicitly passed into the function as input parameters. | ||
- Read from hidden mutable state, i.e., they access non-constant data that was 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 | ||
|
@@ -98,17 +98,16 @@ def double(i: Int): Int = i * 2 | |
|
||
{% endtabs %} | ||
|
||
The next example is bit more tricky. Here, i is not passed as a parameter, but instead referenced directly from the function body. | ||
This works in Scala because functions act as closure - they can capture the state around them. As long as that state is *immutable*, the function is still considered pure. | ||
In this case, the function always returns `6` and each call could be safely replaced with its result. | ||
This concept of closures and "fixing values" is an important tool in functional programming that you will encounter often as you go forward. | ||
The next example is bit more tricky. Here, `i` is not passed as a parameter, but instead referenced directly from the outside. | ||
This works in Scala because functions act as closures - they can capture the state around them. As long as that state is *immutable*, such a closure is still considered pure. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this is the best wording since we usually use "state" to talk about mutable state. Not all of the time. I would suggest "capture values from enclosing scopes". |
||
In this case, the function always returns `6` and each call can be safely replaced with its result. | ||
|
||
{% tabs fp-pure-function-closure %} | ||
|
||
{% tab 'Scala 2 and 3' %} | ||
```scala | ||
val i = 3 | ||
def double(i: Int): Int = i * 2 | ||
def double(): Int = i * 2 | ||
``` | ||
{% endtab %} | ||
|
||
|
@@ -145,7 +144,7 @@ If you understand that code, you’ll see that it meets the pure function defini | |
|
||
The first key point of this section is the definition of a pure function: | ||
|
||
> A _pure function_ is a function that depends only on its declared inputs and its implementation to produce its output. | ||
> A _pure function_ is a function that depends only on its declared inputs, captured constants, and its implementation to produce its output. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest "closed-over values" rather than "captured constants" |
||
> It only computes its output and does not depend on or modify the outside world. | ||
|
||
A second key point is that every real-world application interacts with the outside world. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"non-constant" is not really a synonym for "mutable". I would suggest using the word "mutable" again even if it feels a bit repetitive; it's more important to be precise.