Created
January 30, 2018 23:28
-
-
Save visig9/bd8a7b08a0af2f7b573936c07fa68a2a to your computer and use it in GitHub Desktop.
Minimized pipeline generating function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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