Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. from functools import partial
  2.  
  3. def chain(*fns, args=None):
  4. '''Pass arguments through a series of functions
  5.  
  6. Parameters
  7. ----------
  8. *fns : list of functions
  9. A list of functions to run consecutively
  10. args : object or tuple, optional
  11. An argument that will be passed to the first function
  12.  
  13. Returns
  14. -------
  15. The result of the final function, or a partial function that
  16. takes any number of arguments if the `args` was not set.
  17.  
  18. Example
  19. -------
  20. >>> chain(str.upper, iter, lambda s: ' '.join(s), args='testing')
  21. 'T E S T I N G'
  22. '''
  23.  
  24. if args is None:
  25. return partial(lambda *args: chain(*fns, args=args))
  26. for fn in fns:
  27. if not isinstance(args, tuple):
  28. args = (args,)
  29. args = fn(*args)
  30. return args
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement