Skip to content

Instantly share code, notes, and snippets.

@sshirokov
Created May 22, 2009 03:37
Show Gist options
  • Save sshirokov/115911 to your computer and use it in GitHub Desktop.
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())) )
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