Clojure closures.


(defn make-counter []
  (let [counter (atom 0)]
    {:inc (fn [] (swap! counter inc)) :get (fn [] @counter)}))

(def mycount (make-counter))

((:inc mycount))

((:get mycount))

(def yourcount (make-counter))

((:inc yourcount))
((:inc yourcount))

((:get mycount))
((:get yourcount))

CSci 4651 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.