Skip to content

Home

Reverse compose functions

Performs left-to-right function composition.

from functools import reduce

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

add = lambda x, y: x + y
square = lambda x: x * x
add_and_square = compose_right(add, square)
add_and_square(1, 2) # 9

More like this

Start typing a keyphrase to see matching snippets.