Created
September 15, 2012 14:54
-
-
Save jbrechtel/3728354 to your computer and use it in GitHub Desktop.
Confused with memoize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;not sure why this doesn't really memoize the anonymous function call | |
| (defn add [a b] ((memoize (fn [x y] (Thread/sleep 1000) (+ x y))) a b)) | |
| ;this does correctly memoize the (non anonymous) function call | |
| (defn adds [a b] (Thread/sleep 1000) (+ a b)) | |
| (def addf (memoize adds)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suspect this has to do with the first one recreating the anonymous function every time add is called, but this behavior isn't intuitive to me.
Is it because (= adds adds) is true but (= (fn ...) (fn ...)) is false?