Skip to content

Commit d635ac2

Browse files
committed
Add compose and resources to Readme
1 parent 29be330 commit d635ac2

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1-
# Functional-Programming-Techniques-In-Python
2-
1+
# Functional Programming Techniques In Python
32

43
<p align="center">
54
<img src="https://raw.githubusercontent.com/mpkocher/Functional-Programming-Techniques-In-Python/main/logo.png" />
65
</p>
76

7+
```python
8+
import functools
9+
10+
def compose(*funcs):
11+
"""Functional composition [f, g, h] will be h(g(f(x))) """
12+
def compose_two(f, g):
13+
def c(x):
14+
return f(g(x))
15+
return c
16+
return functools.reduce(compose_two, reversed(funcs))
17+
18+
19+
def adder(a: int, b: int) -> int:
20+
return a + b
21+
22+
23+
def multiply(n: int):
24+
def wrapper(m: int) -> int:
25+
return m * n
26+
return wrapper
27+
28+
add_one = functools.partial(adder, 1)
29+
add_two = functools.partial(adder, 2)
30+
31+
32+
f = compose(add_one, multiply(3), add_two)
33+
assert f(7) == 26
34+
35+
fp = compose(f, print)
36+
fp(7)
37+
```
38+
839
## Exploring functional centric design style and patterns in Python
940

1041

@@ -22,3 +53,12 @@ In [Part 2 (notebook)](https://github.com/mpkocher/Functional-Programming-Techni
2253
If you're a OO wizard, a Data Scientist/Analysist, or a backend dev, this series can be useful to add another design approach in your toolbelt to designing APIs or programs.
2354

2455
[Originally posted here](https://mpkocher.github.io/2019/03/01/Functional-Programming-Techniques-In-Python-Series/)
56+
57+
58+
## Other Resources
59+
60+
- https://github.com/pytoolz/toolz
61+
- https://github.com/dry-python/returns
62+
- https://github.com/getify/Functional-Light-JS
63+
- https://github.com/sfermigier/awesome-functional-python
64+

0 commit comments

Comments
 (0)