diff options
Diffstat (limited to 'memoize.clj')
-rw-r--r-- | memoize.clj | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/memoize.clj b/memoize.clj index dcaf642b..ce59edb6 100644 --- a/memoize.clj +++ b/memoize.clj @@ -20,14 +20,13 @@ (defn memoize "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 + to results and, when calls with the same arguments are repeated often, has higher performance at the expense of higher memory use." [function] (let [cache (ref {})] (fn [& args] - (if-let cached (@cache args) - cached - (let [result (apply function args)] - (dosync - (commute cache assoc args result)) - result))))) + (or (@cache args) + (let [result (apply function args)] + (dosync + (commute cache assoc args result)) + result))))) |