Tutorial by Examples

To define an atom, use an ordinary def, but add an atom function before it, like so: (def counter (atom 0)) This creates an atom of value 0. Atoms can be of any type: (def foo (atom "Hello")) (def bar (atom ["W" "o" "r" "l" "d"])) ...
To read an atom's value, simply put the name of the atom, with a @ before it: @counter ; => 0 A bigger example: (def number (atom 3)) (println (inc @number)) ;; This should output 4
There are two commands to change an atom, swap! and reset!. swap! is given commands, and changes the atom based on its current state. reset! changes the atom's value completely, regardless of what the original atom's value was: (swap! counter inc) ; => 1 (reset! counter 0) ; => 0 This e...

Page 1 of 1