Add compose

This commit is contained in:
Angelos Chalaris
2020-01-02 15:51:20 +02:00
parent 72efadce08
commit 6620e730e1

24
snippets/compose.md Normal file
View File

@ -0,0 +1,24 @@
---
title: compose
tags: function,intermediate
---
Performs right-to-left function composition.
Use `reduce()` to perform right-to-left function composition.
The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.
```py
from functools import reduce
def compose(*fns):
return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
```
```py
add5 = lambda x: x + 5
multiply = lambda x, y: x * y
multiply_and_add_5 = compose(add5, multiply)
multiply_and_add_5(5, 2) # 15
```