Created
April 4, 2012 20:29
-
-
Save Katharine/2305344 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
def cached(f): | |
def wrapper(*args): | |
if args not in wrapper.cache: | |
wrapper.cache[args] = f(*args) | |
return wrapper.cache[args] | |
wrapper.cache = {} | |
return wrapper | |
def factorial(n): | |
print "Running factorial" | |
result = 1 | |
for i in range(2, n + 1): | |
result = result * i | |
return result | |
factorial = cached(factorial) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment