Skip to content
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

clarify wording on pure-functions overview to allow immutable hidden state, and add an example #3134

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

epic-64
Copy link

@epic-64 epic-64 commented Dec 21, 2024

Updates the "pure functions" overview at https://docs.scala-lang.org/scala3/book/fp-pure-functions.html

  • adds clarification that accessing immutable hidden state is still considered pure
  • adds an example of this in form of a closure

@epic-64
Copy link
Author

epic-64 commented Dec 21, 2024

there are still some typos, sorry, marking it as draft until they are fixed

@epic-64 epic-64 closed this Dec 21, 2024
@epic-64 epic-64 reopened this Dec 21, 2024
@epic-64 epic-64 marked this pull request as draft December 21, 2024 17:57
@epic-64 epic-64 marked this pull request as ready for review December 21, 2024 18:04
@@ -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 state, i.e., they access 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
Copy link
Member

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.

@@ -98,6 +98,21 @@ 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 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.
Copy link
Member

Choose a reason for hiding this comment

The 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".

@@ -129,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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest "closed-over values" rather than "captured constants"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants