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
...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
+
>>>assertsorted([2, 1]) == [1, 2]
115
+
>>>assertsorted(sorted([2, 1])) == [1, 2]
116
+
>>>assertsorted(sorted(sorted([2, 1]))) == [1, 2]
117
+
>>>
118
+
```
119
+
120
+
Or:
121
+
122
+
```python
123
+
>>>assertabs(abs(abs(-1))) ==abs(-1)
124
+
>>>
125
+
```
126
+
127
+
66
128
## Arity
67
129
68
130
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
314
376
```
315
377
316
378
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.
...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
-
>>>assertsorted([2, 1]) == [1, 2]
365
-
>>>assertsorted(sorted([2, 1])) == [1, 2]
366
-
>>>assertsorted(sorted(sorted([2, 1]))) == [1, 2]
367
-
>>>
368
-
```
369
-
370
379
## Point-Free Style (TODO)
371
380
372
381
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