Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement