Last active
November 27, 2017 18:19
-
-
Save everling-prime/beb04a6c0439386dc0e775137b77a648 to your computer and use it in GitHub Desktop.
Decorate Python functions with memo to allow for efficient caching of intermediate results (memoization)
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 memo(f): | |
"Memoize function f, whose args must all be hashable." | |
cache = {} | |
def fmemo(*args): | |
if args not in cache: | |
cache[args] = f(*args) | |
return cache[args] | |
fmemo.cache = cache | |
return fmemo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment