8000 Updates `HOF` section · jgarte/functional-jargon-python@926a6f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 926a6f0

Browse files
Updates HOF section
1 parent 1342623 commit 926a6f0

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ __Table of Contents__
1515
* [Purity](#purity)
1616
* [Idempotent](#idempotent)
1717
* [Arity](#arity)
18-
* [Higher-Order Functions (HOF) (TODO)](#higher-order-functions-hof-todo)
18+
* [Higher-Order Functions (HOF)](#higher-order-functions-hof)
1919
* [Closure (TODO)](#closure-todo)
2020
* [Partial Application](#partial-application)
2121
* [Currying](#currying)
@@ -200,12 +200,40 @@ There is a little difference between __arguments__ and __parameters__:
200200
* __arguments__: are the values that are passed to a function
201201
* __parameters__: are the variables in the function definition
202202

203-
## Higher-Order Functions (HOF) (TODO)
203+
## Higher-Order Functions (HOF)
204204

205-
A function which takes a function as an argument and/or returns a function.
205+
A function that takes a function as an argument and/or returns a function, basically we can treat functions as a value.
206+
In Python every function/method is a Higher-Order Function.
206207

208+
The functions like `reduce`, `map` and `filter` are good examples of __HOF__, they receive a function as their first argument.
207209
```python
208-
# TODO
210+
>>> from functools import reduce
211+
212+
>>> reduce(lambda accumulator, number: accumulator + number, [1, 2, 3])
213+
6
214+
>>>
215+
```
216+
217+
We can create our own __HOF__, see the example below:
218+
219+
```python
220+
>>> from typing import Callable, TypeVar
221+
222+
>>> _ValueType = TypeVar('_ValueType')
223+
>>> _ReturnType = TypeVar('_ReturnType')
224+
225+
>>> def get_transform_function() -> Callable[[str], int]:
226+
... return int
227+
228+
>>> def transform(
229+
... transform_function: Callable[[_ValueType], _ReturnType],
230+
... value_to_transform: _ValueType,
231+
... ) -> _ReturnType:
232+
... return transform_function(value_to_transform)
233+
234+
>>> transform_function = get_transform_function()
235+
>>> assert transform(transform_function, '42') == 42
236+
>>>
209237
```
210238

211239
## Closure (TODO)
@@ -378,7 +406,7 @@ Continuations are often seen in asynchronous programming when the program needs
378406

379407
## Point-Free Style (TODO)
380408

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.
409+
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). A.K.A Tacit programming.
382410

383411
```python
384412
# TODO

0 commit comments

Comments
 (0)
0