Skip to content

Instantly share code, notes, and snippets.

@hansonkd
Last active January 17, 2017 02:04
Show Gist options
  • Save hansonkd/5fa8f83983edd84cbcc5825a00127f87 to your computer and use it in GitHub Desktop.
Save hansonkd/5fa8f83983edd84cbcc5825a00127f87 to your computer and use it in GitHub Desktop.
class Obj(object):
def method1(self):
return do_stuff(self)
def method2(self):
return do_other_stuff(self)
def method3(self):
return do_more_stuff(self)
result = Obj().method1().method2().method3()
# Equivalent using function composition.
def compose(*funcs):
return lambda xx: reduce(lambda val, func: func(val), reversed(funcs), xx)
composed = compose(
Obj.method1,
Obj.method2,
Obj.method3
)
result2 = composed(Obj())
assert result == result2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment