diff options
author | scgilardi <scgilardi@gmail.com> | 2008-06-03 04:51:52 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-06-03 04:51:52 +0000 |
commit | 7a236c4033c1f3ef68cb89b4ed5d9ff084521f74 (patch) | |
tree | 4260a140ced9e02215daf413188f58fec1da4d79 | |
parent | 7d3ef537294ca215958098d7215680a52ff6c0ab (diff) |
memoize.clj: simplified
-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))))) |