Skip to content

Instantly share code, notes, and snippets.

@jbrechtel
Created September 15, 2012 14:54
Show Gist options
  • Select an option

  • Save jbrechtel/3728354 to your computer and use it in GitHub Desktop.

Select an option

Save jbrechtel/3728354 to your computer and use it in GitHub Desktop.
Confused with memoize
;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))
@jbrechtel
Copy link
Copy Markdown
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment