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/fp-pure-functions.md
+6-7Lines changed: 6 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ Conversely, the following functions are _impure_ because they violate the defini
57
57
58
58
Impure functions often do one or more of these things:
59
59
60
-
- Read from hidden mutable state, i.e., they access non-constant variables and data not explicitly passed into the function as input parameters.
60
+
- Read from hidden mutable state, i.e., they access non-constant data that was not explicitly passed into the function as input parameters
61
61
- Write to hidden state
62
62
- Mutate the parameters they’re given, or mutate hidden variables, such as fields in their containing class
63
63
- Perform some sort of I/O with the outside world
@@ -98,17 +98,16 @@ def double(i: Int): Int = i * 2
98
98
99
99
{% endtabs %}
100
100
101
-
The next example is bit more tricky. Here, i is not passed as a parameter, but instead referenced directly from the function body.
102
-
This works in Scala because functions act as closure - they can capture the state around them. As long as that state is *immutable*, the function is still considered pure.
103
-
In this case, the function always returns `6` and each call could be safely replaced with its result.
104
-
This concept of closures and "fixing values" is an important tool in functional programming that you will encounter often as you go forward.
101
+
The next example is bit more tricky. Here, `i` is not passed as a parameter, but instead referenced directly from the outside.
102
+
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.
103
+
In this case, the function always returns `6` and each call can be safely replaced with its result.
105
104
106
105
{% tabs fp-pure-function-closure %}
107
106
108
107
{% tab 'Scala 2 and 3' %}
109
108
```scala
110
109
vali=3
111
-
defdouble(i: Int):Int= i *2
110
+
defdouble():Int= i *2
112
111
```
113
112
{% endtab %}
114
113
@@ -145,7 +144,7 @@ If you understand that code, you’ll see that it meets the pure function defini
145
144
146
145
The first key point of this section is the definition of a pure function:
147
146
148
-
> A _pure function_ is a function that depends only on its declared inputs and its implementation to produce its output.
147
+
> A _pure function_ is a function that depends only on its declared inputs, captured constants, and its implementation to produce its output.
149
148
> It only computes its output and does not depend on or modify the outside world.
150
149
151
150
A second key point is that every real-world application interacts with the outside world.
0 commit comments