Skip to content

Instantly share code, notes, and snippets.

@zshipko
Created August 24, 2017 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zshipko/a837439beeed851fab12cd1e56ad7909 to your computer and use it in GitHub Desktop.
Save zshipko/a837439beeed851fab12cd1e56ad7909 to your computer and use it in GitHub Desktop.
from functools import partial
def chain(*fns, args=None):
'''Pass arguments through a series of functions
Parameters
----------
*fns : list of functions
A list of functions to run consecutively
args : object or tuple, optional
An argument that will be passed to the first function
Returns
-------
The result of the final function, or a partial function that
takes any number of arguments if the `args` was not set.
Example
-------
>>> chain(str.upper, iter, lambda s: ' '.join(s), args='testing')
'T E S T I N G'
'''
if args is None:
return partial(lambda *args: chain(*fns, args=args))
for fn in fns:
if not isinstance(args, tuple):
args = (args,)
args = fn(*args)
return args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment