Skip to content

Instantly share code, notes, and snippets.

@everling-prime
Last active November 27, 2017 18:19
Show Gist options
  • Save everling-prime/beb04a6c0439386dc0e775137b77a648 to your computer and use it in GitHub Desktop.
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)
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