Skip to content

Instantly share code, notes, and snippets.

@syou6162
Created November 10, 2012 02:47
Show Gist options
  • Save syou6162/4049645 to your computer and use it in GitHub Desktop.
Save syou6162/4049645 to your computer and use it in GitHub Desktop.
(defmacro memoize-fn [name args body]
`(let [mem# (atom {})]
(fn ~args
(if-let [e# (find @mem# ~args)]
(val e#)
(let [ret# ~body]
(swap! mem# assoc ~args ret#)
ret#)))))
(->> (range 35)
(map (memoize-fn fib [n]
(if (<= n 1)
n
(+ (fib (dec n)) (fib (- n 2))))))
(dorun)
(time))
; "Elapsed time: 1.672 msecs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment