Created
May 23, 2015 02:12
-
-
Save samroberton/75860283ba2579a6452e to your computer and use it in GitHub Desktop.
my-memoize
This file contains 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
(defn expensive-fn [x] | |
(.log js/console (str "I ran (with value " x ")")) | |
(+ x 1)) | |
(defn my-memoize [f] | |
(let [results (atom {})] | |
(fn [farg] | |
(or (get @results farg) | |
(get (swap! results assoc farg (f farg)) farg))))) | |
(defn my-memoize2 [f] | |
(let [r (atom {})] #(or (@r %) ((swap! r assoc % (f %) %))))) | |
(defn app-template | |
[app] | |
(html | |
[:div#container | |
[:h1 (:text app)] | |
[:ul | |
(let [memoized-expensive (my-memoize expensive-fn)] | |
(list | |
[:li (memoized-expensive 1)] | |
[:li (memoized-expensive 1)] | |
[:li (memoized-expensive 1)]))]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment