Skip to content

Update fp-pure-functions.md #2009

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

Merged
merged 2 commits into from
May 10, 2021
Merged

Update fp-pure-functions.md #2009

merged 2 commits into from
May 10, 2021

Conversation

b-studios
Copy link
Contributor

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.

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.
@b-studios b-studios added the scala-3 scala 3 documentation label May 10, 2021
@b-studios b-studios requested a review from vincenzobaz May 10, 2021 11:40
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

Calling getHour on a LocalTime always returns the same value, so this might be confusing. Consider maybe using currentTimeMillis?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks you are, I was just using those since @alvinj mentioned them in the original doc.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, currentTimeMillis is a much more obvious example!

@b-studios b-studios merged commit 85a5601 into main May 10, 2021
@Philippus Philippus deleted the fp-impurity branch May 21, 2021 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scala-3 scala 3 documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants