Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Clojure synchronous/asynchronous state changes
$ clj
Clojure 1.4.0
user=> ;; atoms - synchronous sharing state changes
user=> (def w (atom 0))
#'user/w
user=> (swap! w inc)
1
user=> @w
1
user=> (swap! w #(* % 100))
100
user=> @w
100
user=> ;; agents - asynchronous sharing state changes
user=> (def waiters (agent 0))
#'user/waiters
user=> (send waiters inc)
#<Agent@c3fa6cd: 1>
user=> @waiters
1
user=> (send waiters #(+ % 10))
#<Agent@c3fa6cd: 11>
user=> @waiters
11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment