Skip to content

Commit

Permalink
memoize returns nil on first call
Browse files Browse the repository at this point in the history
if the first call to a memoized fn results in `nil`, the result should
be `nil` instead of `::nil`
  • Loading branch information
dm3 committed Oct 18, 2015
1 parent e644b4a commit 1039cd0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/potemkin/utils.clj
Expand Up @@ -99,7 +99,7 @@
(if-not (nil? v#)
(re-nil v#)
(let [v# (de-nil (~f ~@args))]
(or (.putIfAbsent ~m k# v#) v#))))))
(re-nil (or (.putIfAbsent ~m k# v#) v#)))))))

(defn fast-memoize
"A version of `memoize` which has equivalent behavior, but is faster."
Expand Down
12 changes: 12 additions & 0 deletions test/potemkin/utils_test.clj
Expand Up @@ -15,6 +15,18 @@
(deftest test-try*
)

(deftest fast-memoize-test
(testing "returns nil on first call"
(let [f (fn [x] nil)
f' (fast-memoize f)]
(is (nil? (f' 1)))))

(testing "memoizes"
(let [f' (fast-memoize +)]
(is (= 5 (f' 2 3)))
(is (= 5 (f' 2 3)))
(is (= 6 (f' 2 3 1))))))

(deftest ^:benchmark benchmark-fast-memoize
(let [f (memoize +)
f' (fast-memoize +)]
Expand Down

0 comments on commit 1039cd0

Please sign in to comment.