Menu

gitpiper

compose python Code Snippet in 2024

functionadvanced

Last Updated: 12 February 2024

Performs right-to-left function composition.

  • 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 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

python snippet similar to compose For You in February 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️