= Atoms
Rich Hickey
2015-01-01
:type: reference
:toc: macro
:icons: font
:prevpagehref: agents
:prevpagetitle: Agents
:nextpagehref: reducers
:nextpagetitle: Reducers

ifdef::env-github,env-browser[:outfilesuffix: .adoc]

Atoms provide a way to manage shared, synchronous, independent state. They are a reference type like refs and vars. You create an atom with https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/atom[atom], and can access its state with https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/deref[deref/@]. Like refs and agents, atoms support validators. To change the value of an atom, you can use https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/swap![swap!]. A lower-level https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/compare-and-set![compare-and-set!] is also provided. Changes to atoms are always free of race conditions.

As with all reference types, the intended use of atom is to hold one of Clojure's immutable data structures. And, similar to ref's alter and agent's send, you change the value by applying a function to the old value. This is done in an atomic manner by https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/swap![swap!] Internally, https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/swap![swap!] reads the current value, applies the function to it, and attempts to compare-and-set! it in. Since another thread may have changed the value in the intervening time, it may have to retry, and does so in a spin loop. The net effect is that the value will always be the result of the application of the supplied function to a current value, atomically. However, because the function might be called multiple times, it must be free of side effects.

Atoms are an efficient way to represent some state that will never need to be coordinated with any other, and for which you wish to make synchronous changes (unlike agents, which are similarly independent but asynchronous). A typical usage might be for memoization:

[source,clojure]
----
(defn memoize [f]
  (let [mem (atom {})]
    (fn [& args]
      (if-let [e (find @mem args)]
        (val e)
        (let [ret (apply f args)]
          (swap! mem assoc args ret)
          ret)))))

(defn fib [n]
  (if (<= n 1)
    n
    (+ (fib (dec n)) (fib (- n 2)))))

(time (fib 35))
user=> "Elapsed time: 941.445 msecs"

(def fib (memoize fib))

(time (fib 35))

user=> "Elapsed time: 0.044 msecs"
----

== Related functions

Create an Atom: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/atom[atom]

Examine an Atom: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/deref[deref] _(see also the +@+ <<reader#,reader>> macro)_

Change Atom state: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/swap![swap!] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/reset![reset!] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/swap-vals![swap-vals!] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/reset-vals![reset-vals!]

Validators: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/set-validator![set-validator!] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/get-validator[get-validator]

Watchers: https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/add-watch[add-watch] https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/remove-watch[remove-watch]

