Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MEMEoisation (hardy har har)
def meme(f):
memes = {}
def inner(*args, **kwargs):
key = (args, tuple(sorted(kwargs.items())))
if key in memes:
return memes[key]
ret = f(*args, **kwargs)
memes[key] = ret
return ret
return inner
@meme
def fib(n):
if n <= 2:
return 1
return fib(n - 2) + fib(n - 1)
assert(fib(20) == 6765)
# Executes quickly
print(fib(100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment