O'Reilly logo

Clojure for Domain-specific Languages by Kelker Ryan

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

Agents

Agents are similar to Atoms, except for the fact that changes are made asynchronously. Unlike an Atom, when attempted changes to an Agent fail, you won't know until you check. This is because changes to an Agent happen in a different thread, so the thread trying to make the change won't be aware of anything going on in the Agent.

You create an Agent the same way you would create an Atom, except that, in this case, you'll use the agent form, as shown in the following code snippet:

user> (def -ag (agent []))
#'user/-ag

We need to create a function that'll show us the current state of the -ag agent, as shown in the following code snippet:

user> (defn show-agent [] (dotimes [_ 6] (Thread/sleep 1000) (println “Slept for" _ “second(s).\n" “-ag = ...

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