Snippets Collections
Use functools.reduce() to perform right-to-left function composition. The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.
from functools import reduce

1.def compose(*fns):
  2.return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
EXAMPLES
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
bookmark
dashboard

Mon Apr 20 2020 13:38:08 GMT+0000 (UTC) https://www.30secondsofcode.org/python/s/compose/

#python #python #function #composition

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension