May 2018
Beginner to intermediate
290 pages
6h 43m
English
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 ...
Read now
Unlock full access