memoize

functionsince v0.0-927 clojure.core/memoizeEdit
(memoize f)

Details:

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.


Source docstring:
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.
Source code @ clojurescript:src/main/cljs/cljs/core.cljs
(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)))))