Swapping Maps

Atoms are not limited to storing just numbers: in fact, you can wrap an atom around any Clojure value. Thus a more practical example might look like this:

 (ns inventory)
 
 (​def​ by-title (atom {}))
 
 (​defn​ add-book [{title :title :as book}]
  (swap! by-title #(assoc % title book)))
 
 (​defn​ del-book [title]
  (swap! by-title #(dissoc % title )))
 
 (​defn​ find-book [title]
  (get @by-title title))

That gives us a simple but complete stateful inventory manager:

 (find-book ​"Emma"​) ​; Nope
 
 (add-book {:title ​"1984"​, :copies 1948})
 (add-book {:title ​"Emma"​, :copies 100})
 (del-book ​"1984"​)
 
 (find-book ​"Emma"​) ​; Yup
 (find-book ​"1984"​) ​; Nope

There really is not much to an atom. An atom is a container ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.