-
Notifications
You must be signed in to change notification settings - Fork 1k
Resolving TODO tags, Part 2 #1874
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
Changes from all commits
e82659c
a63de05
523f76f
12b40f0
56d08b8
166bdba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,15 +8,14 @@ next-page: fp-functions-are-values | |
--- | ||
|
||
|
||
{% comment %} | ||
TODO: Use someone else’s definition? | ||
{% endcomment %} | ||
|
||
Another feature that Scala offers to help you write functional code is the ability to write pure functions. | ||
A _pure function_ can be defined like this: | ||
|
||
- A function `f` is pure if, given the same input `x`, it always returns the same output `f(x)` | ||
- The function’s output depends *only* on its input variables and its internal algorithm | ||
- The function’s output depends _only_ on its input variables and its implementation | ||
- It only computes the output and does not modify the world around it | ||
|
||
This implies: | ||
- It doesn’t modify its input parameters | ||
- It doesn’t mutate any hidden state | ||
- It doesn’t have any “back doors”: It doesn’t read data from the outside world (including the console, web services, databases, files, etc.), or write data to the outside world | ||
alvinj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -42,23 +41,23 @@ These `String` methods are also pure functions: | |
|
||
Most methods on the Scala collections classes also work as pure functions, including `drop`, `filter`, `map`, and many more. | ||
|
||
> In Scala, *functions* and *methods* are almost completely interchangeable, so even though we use the common industry term “pure function,” this term can be used to describe both functions and methods. | ||
> In Scala, _functions_ and _methods_ are almost completely interchangeable, so even though we use the common industry term “pure function,” this term can be used to describe both functions and methods. | ||
> If you’re interested in how methods can be used like functions, see the [Eta Expansion][eta] discussion. | ||
|
||
|
||
|
||
## Examples of impure functions | ||
|
||
Conversely, the following functions are *impure* because they violate the definition. | ||
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. | ||
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. In fact, this is not true. It will probably be called with impure functions but 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. Is there any way to reword this, or is it best to just delete the 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. If you want to keep this discussion, here’s a stab at rewording it. FWIW, the main reason I like this discussion is that it helps people keep an eye out for methods that return While the
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. Sorry for not following up earlier. I think your argument is perfectly sensible. Maybe it is easiest to actually say exactly that: "Keep an eye out for methods that return |
||
|
||
> 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. | ||
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. Like foreach above, not completely true. |
||
> Similarly, _any_ method that returns `Unit` is going to be an impure function. | ||
|
||
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. | ||
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. | ||
|
||
|
@@ -111,8 +110,8 @@ 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 internal algorithm to produce its output. | ||
> It does not read any other values from “the outside world”---the world outside of the function’s scope---and it doesn’t modify any values in the outside world. | ||
> A _pure function_ is a function that depends only on its declared inputs and its implementation to produce its output. | ||
> 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. | ||
Therefore, a simplified way to think about functional programs is that they consist of a core of pure functions that are wrapped with other functions that interact 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.