Skip to content

Instantly share code, notes, and snippets.

@jeffchiou
Last active September 2, 2020 15:05
Show Gist options
  • Save jeffchiou/3eaab7646e61f97f4c139e5cfbb1469c to your computer and use it in GitHub Desktop.
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)
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