Permalink
Browse files

CLJS-793: fix memoize (non-truthy values don't get cached)

ClojureScript's memoize fn currently uses `(get @mem args)` to
check for the existence of a cache entry, preventing falsey
values from being cached correctly.

Clojure's `memoize` uses `find` rather than `get` to avoid this
issue. Here we'll use a `get` lookup sentinel since it's faster.
  • Loading branch information...
1 parent 2907190 commit 4bbbd6068af766ab195de99ba07f54dd87e576c0 @ptaoussanis ptaoussanis committed with swannodette Apr 8, 2014
Showing with 6 additions and 5 deletions.
  1. +6 −5 src/cljs/cljs/core.cljs
@@ -7393,11 +7393,12 @@ Maps become Objects. Arbitrary keys are encoded to by key->js."
[f]
(let [mem (atom {})]
(fn [& args]
- (if-let [v (get @mem args)]
- v
- (let [ret (apply f args)]
- (swap! mem assoc args ret)
- ret)))))
+ (let [v (get @mem args lookup-sentinel)]
+ (if (identical? v lookup-sentinel)
+ (let [ret (apply f args)]
+ (swap! mem assoc args ret)
+ ret)
+ v)))))
(defn trampoline
"trampoline can be used to convert algorithms requiring mutual

0 comments on commit 4bbbd60

Please sign in to comment.