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:
IRef | create-fn | update-fn(s) | set-fn |
---|---|---|---|
Atom | atom | swap! | reset! |
Ref | ref | alter, commute | ref-set |
Var | def | alter-var-root | var-set |
Agent | agent | send, send-off | restart-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.