Skip to content

Home

Compose functions

Performs right-to-left function composition.

from functools import reduce

def compose(*fns):
  return reduce(lambda f, g: lambda *args: f(g(*args)), fns)

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

More like this

Start typing a keyphrase to see matching snippets.