8000 Adds docs about purity and side effects · bitnom/functional-jargon-python@b73b3bd · GitHub
[go: up one dir, main page]

Skip to content

Commit b73b3bd

Browse files
authored
Adds docs about purity and side effects
1 parent 4249087 commit b73b3bd

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ __Table of Contents__
1717
* [Auto Currying (TODO)](#auto-currying-todo)
1818
* [Function Composition (TODO)](#function-composition-todo)
1919
* [Continuation (TODO)](#continuation-todo)
20-
* [Purity (TODO)](#purity-todo)
20+
* [Purity](#purity-todo)
2121
* [Side effects (TODO)](#side-effects-todo)
2222
* [Idempotent (TODO)](#idempotent-todo)
2323
* [Point-Free Style (TODO)](#point-free-style-todo)
@@ -168,24 +168,35 @@ Continuations are often seen in asynchronous programming when the program needs
168168
## Purity (TODO)
169169

170170
A function is pure if the return value is only determined by its
171-
input values, and does not produce side effects.
171+
input values, and does not produce any side effects.
172+
173+
This function is pure:
172174

173175
```python
174-
# TODO
176+
def add(first: int, second: int) -> int:
177+
return first + second
175178
```
176179

177180
As opposed to each of the following:
178181

179182
```python
180-
# TODO
183+
def add_and_log(first: int, second: int) -> int:
184+
print('Sum is:', first + second) # print is a side effect
185+
return first + second
181186
```
182187

183188
## Side effects (TODO)
184189

185-
A function or expression is said to have a side effect if apart from returning a value, it interacts with (reads from or writes to) external mutable state.
190+
A function or expression is said to have a side effect if apart from returning a value,
191+
it interacts with (reads from or writes to) external mutable state.
186192

187193
```python
188-
# TODO
194+
result_sums = []
195+
196+
def add(first: int, second: int) -> int:
197+
result_sum = first + second
198+
result_sums.append(result_sum) # this is a side effect
199+
return result_sum
189200
```
190201

191202
## Idempotent (TODO)

0 commit comments

Comments
 (0)
0