Permalink
Browse files

Merge pull request #43 from dm3/fix-memoize

memoize returns nil on first call
  • Loading branch information...
2 parents 8bee189 + 1039cd0 commit e58484420f1d9ee66884fdeffd09286de745a446 @ztellman committed on GitHub Jan 7, 2017
Showing with 13 additions and 1 deletion.
  1. +1 −1 src/potemkin/utils.clj
  2. +12 −0 test/potemkin/utils_test.clj
@@ -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."
@@ -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 +)]

0 comments on commit e584844

Please sign in to comment.