Return to Snippet

Revision: 16729
at August 13, 2009 14:36 by narkisr


Initial Code
(defn make-counter [init-val] 
  (let [c (atom init-val)] 
    {:next #(swap! c inc)
     :reset #(reset! c init-val)}))

(def c (make-counter 10))
-> #'user/c

((c :next))
-> 11

((c :next))
-> 12

((c :reset))
-> 10

Initial URL
http://blog.thinkrelevance.com/2009/8/12/rifle-oriented-programming-with-clojure-2

Initial Description
Using closures in order to encapsulate access to data (in this case the counter is thread safe atom).

Initial Title
Encapsulation in Clojure

Initial Tags


Initial Language
Lisp