Created
May 22, 2009 03:37
-
-
Save sshirokov/115911 to your computer and use it in GitHub Desktop.
Functions to create class methods chain-friendly (as in A.b().c().d()) and a function to create function chains (as in: make_consuming_chain(a, b, c)() #=> c(b(a())) )
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
def make_consuming_chain(*functions, **kwargs): | |
''' | |
Return a function that will call functions in sequence, passing the return down the chain of functions | |
''' | |
return reduce(lambda acc, f: lambda *args, **kwargs: f(acc(*args, **kwargs)), functions) | |
def make_chainable(method): | |
''' | |
Make a method returning none return self | |
''' | |
return lambda self, *args, **kwargs: method(self, *args, **kwargs) or self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment