Files
30-seconds-of-code/snippets/compose_right.md
Angelos Chalaris 0f36c17995 Update explanations
Update explanation in digitize
Update explanation in delay
Update explanation in curry
Update explanation in compose_right
Update explanation in compose
2020-01-03 13:08:00 +02:00

561 B

title, tags
title tags
compose_right function,intermediate

Performs left-to-right function composition.

Use functools.reduce() to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.

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