Skip to content

Instantly share code, notes, and snippets.

@visig9
Created January 30, 2018 23:28
Show Gist options
  • Save visig9/bd8a7b08a0af2f7b573936c07fa68a2a to your computer and use it in GitHub Desktop.
Save visig9/bd8a7b08a0af2f7b573936c07fa68a2a to your computer and use it in GitHub Desktop.
Minimized pipeline generating function.
def pipe(*functions):
"""Generate a pipeline with multiple functions.
Arguments:
*functions: multiple callables.
Returns:
A composed function.
Examples:
func = pipe(lambda x: x*2, lambda x: x+1)
print(func(10)) # 21
print(func(3)) # 7
"""
def compose(f1, f2):
return lambda x: f2(f1(x))
return functools.reduce(compose, functions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment