8000 Update README.md · jgarte/functional-jargon-python@1342623 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1342623

Browse files
authored
Update README.md
1 parent 384545c commit 1342623

File tree

1 file changed

+65
-56
lines changed

1 file changed

+65
-56
lines changed

README.md

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ This document is WIP and pull requests are welcome!
1111
__Table of Contents__
1212
<!-- RM(noparent,notop) -->
1313

14+
* [Side effects](#side-effects)
15+
* [Purity](#purity)
16+
* [Idempotent](#idempotent)
1417
* [Arity](#arity)
1518
* [Higher-Order Functions (HOF) (TODO)](#higher-order-functions-hof-todo)
1619
* [Closure (TODO)](#closure-todo)
1720
* [Partial Application](#partial-application)
1821
* [Currying](#currying)
1922
* [Function Composition](#function-composition)
2023
* [Continuation (TODO)](#continuation-todo)
21-
* [Side effects](#side-effects)
22-
* [Purity](#purity)
23-
* [Idempotent](#idempotent)
2424
* [Point-Free Style (TODO)](#point-free-style-todo)
2525
* [Predicate (TODO)](#predicate-todo)
2626
* [Contracts (TODO)](#contracts-todo)
@@ -63,6 +63,68 @@ __Table of Contents__
6363

6464
<!-- /RM -->
6565

66+
67+
## Side effects
68+
69+
A function or expression is said to have a side effect if apart from returning a value,
70+
it interacts with (reads from or writes to) external mutable state:
71+
72+
```python
73+
>>> print('This is a side effect!')
74+
This is a side effect!
75+
>>>
76+
```
77+
78+
Or:
79+
80+
```python
81+
numbers = []
82+
numbers.append(1) # mutates the `numbers` array
83+
```
84+
85+
86+
## Purity
87+
88+
A function is pure if the return value is only determined by its
89+
input values, and does not produce any side effects.
90+
91+
This function is pure:
92+
93+
```python
94+
>>> def add(first: int, second: int) -> int:
95+
... return first + second
96+
>>>
97+
```
98+
99+
As opposed to each of the following:
100+
101+
```python
102+
>>> def add_and_log(first: int, second: int) -> int:
103+
... print('Sum is:', first + second) # print is a side effect
104+
... return first + second
105+
>>>
106+
```
107+
108+
109+
## Idempotent
110+
111+
A function is idempotent if reapplying it to its result does not produce a different result:
112+
113+
```python
114+
>>> assert sorted([2, 1]) == [1, 2]
115+
>>> assert sorted(sorted([2, 1])) == [1, 2]
116+
>>> assert sorted(sorted(sorted([2, 1]))) == [1, 2]
117+
>>>
118+
```
119+
120+
Or:
121+
122+
```python
123+
>>> assert abs(abs(abs(-1))) == abs(-1)
124+
>>>
125+
```
126+
127+
66128
## Arity
67129

68130
The number of arguments a function takes. From words like unary, binary, ternary, etc. This word has the distinction of being composed of two suffixes, "-ary" and "-ity". Addition, for example, takes two arguments, and so it is defined as a binary function or a function with an arity of two. Such a function may sometimes be called "dyadic" by people who prefer Greek roots to Latin. Likewise, a function that takes a variable number of arguments is called "variadic," whereas a binary function must be given two and only two arguments, currying and partial application notwithstanding.
@@ -314,59 +376,6 @@ Continuations are often seen in asynchronous programming when the program needs
314376
```
315377

316378

317-
## Side effects
318-
319-
A function or expression is said to have a side effect if apart from returning a value,
320-
it interacts with (reads from or writes to) external mutable state:
321-
322-
```python
323-
>>> print('This is a side effect!')
324-
This is a side effect!
325-
>>>
326-
```
327-
328-
Or:
329-
330-
```python
331-
numbers = []
332-
numbers.append(1) # mutates the `numbers` array
333-
```
334-
335-
336-
## Purity
337-
338-
A function is pure if the return value is only determined by its
339-
input values, and does not produce any side effects.
340-
341-
This function is pure:
342-
343-
```python
344-
>>> def add(first: int, second: int) -> int:
345-
... return first + second
346-
>>>
347-
```
348-
349-
As opposed to each of the following:
350-
351-
```python
352-
>>> def add_and_log(first: int, second: int) -> int:
353-
... print('Sum is:', first + second) # print is a side effect
354-
... return first + second
355-
>>>
356-
```
357-
358-
359-
## Idempotent
360-
361-
A function is idempotent if reapplying it to its result does not produce a different result:
362-
363-
```python
364-
>>> assert sorted([2, 1]) == [1, 2]
365-
>>> assert sorted(sorted([2, 1])) == [1, 2]
366-
>>> assert sorted(sorted(sorted([2, 1]))) == [1, 2]
367-
>>>
368-
```
369-
370379
## Point-Free Style (TODO)
371380

372381
Writing functions where the definition does not explicitly identify the arguments used. This style usually requires [currying](#currying) or other [Higher-Order functions](#higher-order-functions-hof-todo). A.K.A Tacit programming.

0 commit comments

Comments
 (0)
0