Skip to content

Instantly share code, notes, and snippets.

@borntyping
Created July 24, 2018 09:32
Show Gist options
  • Save borntyping/836be11878624a3c3458b3f47c92b8c9 to your computer and use it in GitHub Desktop.
Save borntyping/836be11878624a3c3458b3f47c92b8c9 to your computer and use it in GitHub Desktop.
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