You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/scala3-book/domain-modeling-fp.md
+28Lines changed: 28 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -177,6 +177,34 @@ To compute the price of the crust we simultaneously pattern match on both the si
177
177
> 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).
178
178
> All they do is simply receive values and compute the result.
179
179
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):
- 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
+
180
208
## How to Organize Functionality
181
209
When implementing the `pizzaPrice` function above, we did not say _where_ we would define it.
182
210
In Scala 3, it would be perfectly valid to define it on the toplevel of your file.
Copy file name to clipboardExpand all lines: _overviews/scala3-book/fp-pure-functions.md
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -13,9 +13,11 @@ TODO: Use someone else’s definition?
13
13
{% endcomment %}
14
14
15
15
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:
17
17
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
19
21
- It doesn’t mutate any hidden state
20
22
- 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
0 commit comments