Skip to content

Commit 6b470ff

Browse files
committed
Working to resolve the Pure Function definition
1 parent f6a545b commit 6b470ff

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

_overviews/scala3-book/domain-modeling-fp.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,34 @@ To compute the price of the crust we simultaneously pattern match on both the si
177177
> An important point about all functions shown above is that they are *pure functions*: they do not mutate any data or have other side-effects (like throwing exceptions or writing to a file).
178178
> All they do is simply receive values and compute the result.
179179
180+
{% comment %}
181+
I’ve added this comment per [this Github comment](https://github.com/scalacenter/docs.scala-lang/pull/3#discussion_r543372428).
182+
To that point, I’ve added these definitions here from our Slack conversation, in case anyone wants to update the “pure function” definition. If not, please delete this comment.
183+
184+
Sébastien:
185+
----------
186+
A function `f` is pure if, given the same input `x`, it will always return the same output `f(x)`, and it never modifies any state outside of it (therefore potentially causing other functions to behave differently in the future).
187+
188+
Jonathan:
189+
---------
190+
We say a function is 'pure' if it does not depend on or modify the context it is called in.
191+
192+
Wikipedia
193+
---------
194+
The function always evaluates to the same result value given the same argument value(s). It cannot depend on any hidden state or value, and it cannot depend on any I/O.
195+
Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.
196+
197+
Mine (Alvin, now modified, from fp-pure-functions.md):
198+
------------------------------------------------------
199+
- A function `f` is pure if, given the same input `x`, it always returns the same output `f(x)`
200+
- The function’s output depends *only* on its input variables and its internal algorithm
201+
- It doesn’t modify its input parameters
202+
- It doesn’t mutate any hidden state
203+
- 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
204+
{% endcomment %}
205+
206+
207+
180208
## How to Organize Functionality
181209
When implementing the `pizzaPrice` function above, we did not say _where_ we would define it.
182210
In Scala 3, it would be perfectly valid to define it on the toplevel of your file.

_overviews/scala3-book/fp-pure-functions.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ TODO: Use someone else’s definition?
1313
{% endcomment %}
1414

1515
Another feature that Scala offers to help you write functional code is the ability to write pure functions.
16-
In his book, *Functional Programming, Simplified*, Alvin Alexander defines a *pure function* like this:
16+
A _pure function_ can be defined like this:
1717

18-
- A function’s output depends *only* on its input variables and its internal algorithm
18+
- A function `f` is pure if, given the same input `x`, it always returns the same output `f(x)`
19+
- The function’s output depends *only* on its input variables and its internal algorithm
20+
- It doesn’t modify its input parameters
1921
- It doesn’t mutate any hidden state
2022
- 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
2123

0 commit comments

Comments
 (0)