Last active
May 18, 2018 22:34
Clojure synchronous/asynchronous state changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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