Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def cache(func):
- 'Optimize a pure function by caching prior results'
- prior_results = {}
- @wraps(func)
- def newfunc(*args):
- if args in prior_results:
- return prior_results[args]
- result = func(*args)
- prior_results[args] = result
- return result
- return newfunc
- @cache
- def fibo(n):
- if n == 0: return 0
- if n == 1: return 1
- return fibo(n-1) + fibo(n-2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement