O'Reilly logo

Clojure Applied by Alex Miller, Ben Vandgrift

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Tools for Managing Change

Clojure has four reference types (var, atom, agent, and ref) that we can use to store application state. In every case, the mechanism provides a mutable container storing an immutable value. We can create the container with an initial value and reset that value. We can also advance the state using the unified update model. In this fashion, we can change the application state in a managed way.

Clojure’s reference types implement IRef. The following table lists those types along with their create, update, and reset functions:

IRefcreate-fnupdate-fn(s)set-fn
Atomatomswap!reset!
Refrefalter, commuteref-set
Vardefalter-var-rootvar-set
Agentagentsend, send-offrestart-agent

The pattern for all of these reference types is similar: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required