Skip to content

Instantly share code, notes, and snippets.

@skrat
Created June 28, 2016 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrat/d29203738a3e855ff7eab4df7fc9e470 to your computer and use it in GitHub Desktop.
Save skrat/d29203738a3e855ff7eab4df7fc9e470 to your computer and use it in GitHub Desktop.
mem.cljs
(defn memoize-async
"Returns a memoized version of a referentially transparent function. The
memoized version of the function keeps a cache of the mapping from arguments
to results and, when calls with the same arguments are repeated often, has
higher performance at the expense of higher memory use.
http://stackoverflow.com/questions/24698536/"
[f]
(let [mem (atom {})]
(fn [& args]
(go
(let [v (get @mem args lookup-sentinel)]
(if (identical? v lookup-sentinel)
(let [ret (<! (apply f args))]
(print "MISS" args (contains? (set (keys @mem)) args))
(swap! mem assoc args ret)
ret)
(do (print "HIT!") v)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment