Staying Out of Trouble

Programmers new to Clojure sometimes get lost in expressions like this:

 (​def​ title-atom (atom ​"Pride and Prejudice"​))

The thing to keep in mind is that this expression creates two value containers. The first is a var, which gets bound to the name title-atom. The value of that var is another container, an atom. And inside the atom we find the string "Pride and Prejudice".

If we swap! another value in, perhaps like this, the var doesn’t change at all:

 (swap! title-atom #(str % ​" and Zombies"​))

It is still the same var, bound to the same name and pointing at the same atom. It’s the value inside the atom that gains some zombies.

Another thing to keep in mind is what happens should an update function fail. The good ...

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.