April 2012
Intermediate to advanced
296 pages
7h 3m
English
Atoms are a lighter-weight mechanism than refs. Where multiple ref updates can be coordinated in a transaction, atoms allow updates of a single value, uncoordinated with anything else.
You create atoms with atom, which has a signature very similar to ref:
(atom initial-state options?) | |
; options include: | |
; :validator validate-fn | |
; :meta metadata-map |
Returning to our music player example, you could store the current-track in an atom instead of a ref:
(def current-track (atom "Venus, the Bringer of Peace")) | |
-> #'user/current-track |
You can dereference an atom to get its value, just as you would a ref:
(deref current-track) | |
-> "Venus, the Bringer of Peace" |
@current-track ... |
Read now
Unlock full access