function | since v0.0-927 | clojure.core/memoize | Edit |
(memoize f)
Returns a memoized version of a referentially transparent function.
A memoized version of a function keeps a cache of the mappings from arguments to results in memory. When calls with the same arguments are repeated often, a memoized function has higher performance at the expense of higher memory usage.
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.
(defn memoize
[f]
(let [mem (atom {})]
(fn [& args]
(let [v (get @mem args lookup-sentinel)]
(if (identical? v lookup-sentinel)
(let [ret (apply f args)]
(swap! mem assoc args ret)
ret)
v)))))