math_doodling/math_doodling/utils.py

13 lines
362 B
Python

def cache(limit):
def _cache(old_slow_function):
memory = {}
def new_faster_function(n):
if n in memory:
return memory[n]
result = old_slow_function(n)
if len(memory) < limit:
memory[n] = result
return result
return new_faster_function
return _cache