-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpatterns_page_2.clj
More file actions
27 lines (22 loc) · 872 Bytes
/
patterns_page_2.clj
File metadata and controls
27 lines (22 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
;; managing state
;; agents
;; --------------
(def score (agent 0))
(deref score) ;; 0
@score ;; 0
;; an agent is in charge of managing state.
;; agents guarantee that changes happen in the order they arrive.
(send score inc) ;; #<Agent@6d8f3cc5: 0>
;; send is async.
;; you have to send a function to the agent, which will be applied by
;; the agent, with its current state as a argument.
@score ;; 1
(send score + 100)
;; the first argument to the update function is always the state
;; an agent manages, but additional arguments to the function can be
;; supplied.
(send-off some-agent some-io-bound-fn)
;; Clojure uses a thead pool of cpu-count + 1 for send calls.
;; Instead, when using send-off, Clojure uses a separate thread, since
;; having a lot of i/o-bound threads in one pool can block the pool unnecessarily.
;; so use send-off for i/o-bound functions.