Saturday, 11 August 2012

Atoms

Practicality

Atoms are uncoordinated and synchronous reference types. Their name comes from the atomic nature. The most common way of manipulating them is through swap!.

(def sarah (atom {:name "Sarah" :age 25 :wears-glasses? false}))

(swap! sarah update-in [:age] + 3)

(swap! sarah (comp #(update-in % [:age] inc) #(assoc % :wears-glasses? true)))

Should an atom change during the swap! operation, it will automatically retry the operation. It will keep retrying until it updates undisturbed.

Reset!

reset is a way of changing an atoms value with no regard to what its current value is.

(reset! sarah :y)
:y


No comments:

Post a Comment