Created
July 24, 2018 09:32
-
-
Save borntyping/836be11878624a3c3458b3f47c92b8c9 to your computer and use it in GitHub Desktop.
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
class Chain: | |
def __init__(self): | |
self.functions = [] | |
def and_then(self, func): | |
self.functions.append(func) | |
return self | |
def __call__(self, value): | |
result = value | |
for func in self.functions: | |
result = func(result) | |
return result | |
add = lambda x: lambda y: x+y | |
times = lambda x: lambda y: x*y | |
print(Chain().and_then(add(1)).and_then(times(2))(3)) | |
# 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment