8000 Updates `arity` section (#2) · bitnom/functional-jargon-python@b093f22 · GitHub
[go: up one dir, main page]

Skip to content

Commit b093f22

Browse files
Updates arity section (dry-python#2)
1 parent 275fa8a commit b093f22

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

README.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This document is WIP and pull requests are welcome!
99
__Table of Contents__
1010
<!-- RM(noparent,notop) -->
1111

12-
* [Arity (TODO)](#arity-todo)
12+
* [Arity](#arity)
1313
* [Higher-Order Functions (HOF) (TODO)](#higher-order-functions-hof-todo)
1414
* [Closure (TODO)](#closure-todo)
1515
* [Partial Application (TODO)](#partial-application-todo)
@@ -62,14 +62,77 @@ __Table of Contents__
6262

6363
<!-- /RM -->
6464

65-
## Arity (TODO)
65+
## Arity
6666

67-
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 (see below).
67+
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.
68+
69+
We can use the `inspect` module to know the arity of a function, see the example below:
6870

6971
```python
70-
# TODO
72+
>>> from inspect import signature
73+
74+
>>> def multiply(number_one: int, number_two: int) -> int: # arity 2
75+
... return number_one * number_two
76+
77+
>>< 8000 span class="pl-k">> assert len(signature(multiply).parameters) == 2
78+
```
79+
80+
### Arity Distinctions
81+
82+
#### Minimum Arity and Maximum Arity
83+
84+
The __minimum arity__ is the smallest number of arguments the function expects to work, the __maximum arity__ is the largest number of arguments function can take. Generally, these numbers are different when our function has default parameter values.
85+
86+
```python
87+
>>> from inspect import getfullargspec
88+
>>> from typing import Any
89+
90+
>>> def example(a: Any, b: Any, c: Any = None) -> None: # mim arity: 2 | max arity: 3
91+
... pass
92+
93+
>>> example_args_spec = getfullargspec(example)
94+
>>> max_arity = len(example_args_spec.args)
95+
>>> min_arity = max_arity - len(example_args_spec.defaults)
96+
97+
>>> assert max_arity == 3
98+
>>> assert min_arity == 2
99+
```
100+
101+
#### Fixed Arity and Variable Arity
102+
103+
A function has __fixed arity__ when you have to call it with the same number of arguments as the number of its parameters and a function has __variable arity__ when you can call it with variable number of arguments, like functions with default parameters values.
104+
105+
```python
106+
>>> from typing import Any
107+
108+
>>> def fixed_arity(a: Any, b: Any) -> None: # we have to call with 2 arguments
109+
... pass
110+
111+
>>> def variable_arity(a: Any, b: Any = None) -> None: # we can call with 1 or 2 arguments
112+
... pass
71113
```
72114

115+
#### Definitive Arity and Indefinite Arity
116+
117+
When a function can receive a finite number of arguments it has __definitive arity__, otherwise if the function can receive an undefined number of arguments it has __indefinite arity__. We can reproduce the __indefinite arity__ using Python _*args_ and _**kwargs_, see the example below:
118+
119+
```python
120+
>>> from typing import Any
121+
122+
>>> def definitive_arity(a: Any, b: Any = None) -> None: # we can call just with 1 or 2 arguments
123+
... pass
124+
125+
>>> def indefinite_arity(*args: Any, **kwargs: Any) -> None: # we can call with how many arguments we want
126+
... pass
127+
```
128+
129+
### Arguments vs Parameters
130+
131+
There is a little difference between __arguments__ and __parameters__:
132+
133+
* __arguments__: are the values that are passed to a function
134+
* __parameters__: are the variables in the function definition
135+
73136
## Higher-Order Functions (HOF) (TODO)
74137

75138
A function which takes a function as an argument and/or returns a function.

0 commit comments

Comments
 (0)
0