@@ -76,6 +76,7 @@ We can use the `inspect` module to know the arity of a function, see the example
76
76
... return number_one * number_two
77
77
78
78
>> > assert len (signature(multiply).parameters) == 2
79
+ >> >
79
80
```
80
81
81
82
### Arity Distinctions
@@ -97,6 +98,7 @@ The __minimum arity__ is the smallest number of arguments the function expects t
97
98
98
99
>> > assert max_arity == 3
99
100
>> > assert min_arity == 2
101
+ >> >
100
102
```
101
103
102
104
#### Fixed Arity and Variable Arity
@@ -111,6 +113,7 @@ A function has __fixed arity__ when you have to call it with the same number of
111
113
112
114
>> > def variable_arity (a : Any, b : Any = None ) -> None : # we can call with 1 or 2 arguments
113
115
... pass
116
+ >> >
114
117
```
115
118
116
119
#### Definitive Arity and Indefinite Arity
@@ -125,6 +128,7 @@ When a function can receive a finite number of arguments it has __definitive ari
125
128
126
129
>> > def indefinite_arity (* args : Any, ** kwargs : Any) -> None : # we can call with how many arguments we want
127
130
... pass
131
+ >> >
128
132
```
129
133
130
134
### Arguments vs Parameters
@@ -182,6 +186,7 @@ You can also use `functools.partial` or `returns.curry.partial` to partially app
182
186
>> > assert partial(takes_three_arguments, 1 , 2 )(3 ) == 6
183
187
>> > assert partial(takes_three_arguments, 1 )(2 , 3 ) == 6
184
188
>> > assert partial(takes_three_arguments, 1 , 2 , 3 )() == 6
189
+ >> >
185
190
```
186
191
187
192
The difference between ` returns.curry.partial ` and ` functools.partial `
@@ -218,6 +223,7 @@ Each time the function is called it only accepts one argument and returns a func
218
223
... return a + b + c
219
224
220
225
>> > assert takes_three_args(1 )(2 )(3 ) == 6
226
+ >> >
221
227
```
222
228
223
229
Some implementations of curried functions
@@ -227,6 +233,7 @@ can also take several of arguments instead of just a single argument:
227
233
>> > assert takes_three_args(1 , 2 )(3 ) == 6
228
234
>> > assert takes_three_args(1 )(2 , 3 ) == 6
229
235
>> > assert takes_three_args(1 , 2 , 3 ) == 6
236
+ >> >
230
237
```
231
238
232
239
Let's see what type ` takes_three_args ` has to get a better understanding of its features:
@@ -257,6 +264,7 @@ For example, you can compose `abs` and `int` functions like so:
257
264
258
265
``` python
259
266
>> > assert abs (int (' -1' )) == 1
267
+ >> >
260
268
```
261
269
262
270
You can also create a third function
@@ -276,13 +284,15 @@ that will have an input of the first one and an output of the second one:
276
284
... return lambda argument : second(first(argument))
277
285
278
286
>> > assert compose(int , abs )(' -1' ) == 1
287
+ >> >
279
288
```
280
289
281
290
We already have this functions defined as ` returns.functions.compose ` !
282
291
283
292
``` python
284
293
>> > from returns.functions import compose
285
294
>> > assert compose(bool , str )([]) == ' False'
295
+ >> >
286
296
```
287
297
288
298
__ Further reading__
@@ -310,7 +320,8 @@ A function or expression is said to have a side effect if apart from returning a
310
320
it interacts with (reads from or writes to) external mutable state:
311
321
312
322
``` python
313
- print (' This is a side effect!' )
323
+ >> > print (' This is a side effect!' )
324
+ This is a side effect!
314
325
```
315
326
316
327
Or:
@@ -331,6 +342,7 @@ This function is pure:
331
342
``` python
332
343
>> > def add (first : int , second : int ) -> int :
333
344
... return first + second
345
+ >> >
334
346
```
335
347
336
348
As opposed to each of the following:
@@ -339,6 +351,7 @@ As opposed to each of the following:
339
351
>> > def add_and_log (first : int , second : int ) -> int :
340
352
... print (' Sum is:' , first + second) # print is a side effect
341
353
... return first + second
354
+ >> >
342
355
```
343
356
344
357
@@ -350,6 +363,7 @@ A function is idempotent if reapplying it to its result does not produce a diffe
350
363
>> > assert sorted ([2 , 1 ]) == [1 , 2 ]
351
364
>> > assert sorted (sorted ([2 , 1 ])) == [1 , 2 ]
352
365
>> > assert sorted (sorted (sorted ([2 , 1 ]))) == [1 , 2 ]
366
+ >> >
353
367
```
354
368
355
369
## Point-Free Style (TODO)
0 commit comments