/ Published in: Lisp

Using closures in order to encapsulate access to data (in this case the counter is thread safe atom).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
(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
URL: http://blog.thinkrelevance.com/2009/8/12/rifle-oriented-programming-with-clojure-2
Comments
