Created
August 18, 2014 10:42
-
-
Save mietek/e9e05d93e2517073017b to your computer and use it in GitHub Desktop.
Laziness in Python 2.x
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
#!/usr/bin/env python | |
def force(thunk): | |
return thunk() | |
class delay: | |
def __init__(self, func): | |
self.func = func | |
def remember(): | |
result = self.func() | |
def recall(): | |
return result | |
self.thunk['func'] = recall | |
return result | |
self.thunk = {} | |
self.thunk['func'] = remember | |
def __call__(self): | |
return self.thunk['func']() | |
def hello(): | |
print "hello" | |
thunk = delay(hello) | |
force(thunk) | |
force(thunk) | |
force(thunk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment