Last active
January 17, 2017 02:04
-
-
Save hansonkd/5fa8f83983edd84cbcc5825a00127f87 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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