Use let scope to bing the agent
This commit is contained in:
parent
00385eee11
commit
c8f711e209
1 changed files with 25 additions and 24 deletions
|
|
@ -1,31 +1,32 @@
|
||||||
(def agent-example
|
|
||||||
"Agents provide shared access to mutable state."
|
|
||||||
(agent 10))
|
|
||||||
|
|
||||||
(meditations
|
(meditations
|
||||||
"Creating an Agent is as simple as assigning it a value"
|
(let [agent-example (agent 10)]
|
||||||
(= __ (instance? clojure.lang.Agent agent-example))
|
|
||||||
|
|
||||||
"To get agent's value you dereference it"
|
"Creating an Agent is as simple as assigning it a value"
|
||||||
(= __ @agent-example)
|
(= __ (instance? clojure.lang.Agent agent-example))
|
||||||
|
|
||||||
"To change agent's value you use send function - you pass action and action's arguments to it.
|
"To get agent's value you dereference it"
|
||||||
|
(= __ @agent-example)
|
||||||
|
|
||||||
|
"To change agent's value you use send function - you pass action and action's arguments to it.
|
||||||
Send function, returns immediately, but the action is prccessed in a separate thread and it's the result,
|
Send function, returns immediately, but the action is prccessed in a separate thread and it's the result,
|
||||||
of that action that gets assigned to agent's state"
|
of that action that gets assigned to agent's state"
|
||||||
(= __ (do
|
(= __ (do
|
||||||
(send agent-example + 2)
|
(send agent-example + 2)
|
||||||
@agent-example))
|
@agent-example))
|
||||||
|
|
||||||
"You can't just send a value to agent"
|
"You can't just send a value to agent"
|
||||||
(= __ (do
|
(= __ (do
|
||||||
(set-error-mode! agent-example :continue)
|
(set-error-mode! agent-example :continue)
|
||||||
(send agent-example 20)
|
(send agent-example 20)
|
||||||
@agent-example))
|
@agent-example))
|
||||||
|
|
||||||
"You can create validations for states that agent is allowed to take"
|
"You can create validations for states that agent is allowed to take"
|
||||||
(= __ (do
|
(= __ (do
|
||||||
(set-validator! agent-example #(even? %))
|
(set-validator! agent-example #(even? %))
|
||||||
(try
|
(try
|
||||||
(send agent-example + 1)
|
(send agent-example + 1)
|
||||||
(catch IllegalStateException e))
|
(catch IllegalStateException e))
|
||||||
@agent-example)))
|
@agent-example))
|
||||||
|
|
||||||
|
"It's a good idea to clean up any not finished agents after you are done"
|
||||||
|
(shutdown-agents)))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue