Skip to content

Instantly share code, notes, and snippets.

@samroberton
Created May 23, 2015 02:12
Show Gist options
  • Save samroberton/75860283ba2579a6452e to your computer and use it in GitHub Desktop.
Save samroberton/75860283ba2579a6452e to your computer and use it in GitHub Desktop.
my-memoize
(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