Skip to content

Instantly share code, notes, and snippets.

@kachayev
Last active May 18, 2018 22:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kachayev/4632568 to your computer and use it in GitHub Desktop.
Save kachayev/4632568 to your computer and use it in GitHub Desktop.
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