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: ...

Get Clojure Applied 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.