Created
August 24, 2017 02:58
-
-
Save zshipko/a837439beeed851fab12cd1e56ad7909 to your computer and use it in GitHub Desktop.
This file contains 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 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