Variable in Clojure

1

I would like to have a counter in memory. A variable that at any time I can add or subtract and when I want I access the current value of it.

How to do this in Clojure?

    
asked by anonymous 01.10.2016 / 21:16

1 answer

4

A simple way is to use atom to keep the current value:

(def cnt (atom 0))
(doseq [i (range 5) ]
  (swap! cnt inc)
  (println "value=" @cnt))

value= 1
value= 2
value= 3
value= 4
value= 5

See link and link

    
01.10.2016 / 22:59