Last active
January 18, 2025 18:15
-
-
Save jeffchiou/3eaab7646e61f97f4c139e5cfbb1469c to your computer and use it in GitHub Desktop.
Python pipeline / chaining that takes functions with multiple arguments (as long as the current function returns the correct iterable for the next function's arguments)
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
from functools import reduce | |
# Readable version | |
def pipe(*funcs): | |
def shortpipe(f,g): | |
return lambda *args: g(*f(*args)) | |
return reduce(shortpipe, funcs) | |
# Short version | |
def pipe(*fs): | |
return reduce(lambda f,g: lambda *xs: g(*f(*xs)), fs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment