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
+18-2Lines changed: 18 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Another feature that Scala offers to help you write functional code is the abili
13
13
A _pure function_ can be defined like this:
14
14
15
15
- A function `f` is pure if, given the same input `x`, it always returns the same output `f(x)`
16
-
- The function’s output depends _only_ on its input variables and its implementation
16
+
- The function’s output depends _only_ on its input variables and its implementation (and in case of a closure, any immutable data that it captures)
17
17
- It only computes the output and does not modify the world around it
18
18
19
19
This implies:
@@ -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 state, i.e., they access variables and data not explicitly passed into the function as input parameters
60
+
- Read from hidden mutable state, i.e., they access non-constant variables and data 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,6 +98,22 @@ 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.
105
+
106
+
{% tabs fp-pure-function-closure %}
107
+
108
+
{% tab 'Scala 2 and 3' %}
109
+
```scala
110
+
vali=3
111
+
defdouble(i: Int):Int= i *2
112
+
```
113
+
{% endtab %}
114
+
115
+
{% endtabs %}
116
+
101
117
If you’re comfortable with recursion, here’s a pure function that calculates the sum of a list of integers:
0 commit comments