This file contains hidden or 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
cache = {} | |
def memoize(func): | |
"This is a decorator." | |
def _inner(num): | |
if num in cache: | |
value = cache[num] | |
else: | |
value = func(num) | |
cache[num] = value |
This file contains hidden or 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
function_map = {} | |
def register_doer(name): | |
"This is a decorator factory. It should create and return a decorator" | |
def inner(func): | |
function_map[name] = func | |
return func | |
return inner | |
@register_doer("Do Flibble!") |
This file contains hidden or 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
(import [collections [OrderedDict]]) | |
(defn lru-cache [&optional [limit 100]] | |
(defn inner [func] | |
(setv cache (OrderedDict)) | |
(defn cached-fn [&rest args] | |
(let [[result nil]] | |
(try | |
(setv result (.pop cache args)) | |
(catch [e KeyError] |