Merge 7fecd24dd6 into 2eb2dfe36a
This commit is contained in:
commit
8e8d2f73ed
3 changed files with 38 additions and 2 deletions
|
|
@ -1,7 +1,6 @@
|
|||
Concepts / Language Features
|
||||
=====
|
||||
new record syntax
|
||||
Agents
|
||||
Vars
|
||||
state identity lifetime
|
||||
Metadata
|
||||
|
|
|
|||
|
|
@ -203,4 +203,9 @@
|
|||
5
|
||||
:hello
|
||||
(6 :this :are)
|
||||
]}]]
|
||||
]}]
|
||||
["21_agents" {"__" [true
|
||||
10
|
||||
10
|
||||
12
|
||||
12]}]]
|
||||
|
|
|
|||
32
src/koans/21_agents.clj
Normal file
32
src/koans/21_agents.clj
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
(meditations
|
||||
(let [agent-example (agent 10)]
|
||||
|
||||
"Creating an Agent is as simple as assigning a value to it"
|
||||
(= __ (instance? clojure.lang.Agent agent-example))
|
||||
|
||||
"To get agent's value you dereference it"
|
||||
(= __ @agent-example)
|
||||
|
||||
"To change agent's value you can use 'send' function. Send takes as parameters, agent, action and arguments,
|
||||
it returns immediately but the action is prccessed in a separate thread and it's the result, of action,
|
||||
that gets assigned to agent's state"
|
||||
(= __ (do
|
||||
(send agent-example + 2)
|
||||
@agent-example))
|
||||
|
||||
"You can't just send a value to agent it needs to be a function"
|
||||
(= __ (do
|
||||
(set-error-mode! agent-example :continue)
|
||||
(send agent-example 20)
|
||||
@agent-example))
|
||||
|
||||
"You can create validations for states that agent is allowed to take"
|
||||
(= __ (do
|
||||
(set-validator! agent-example #(even? %))
|
||||
(try
|
||||
(send agent-example + 1)
|
||||
(catch IllegalStateException e))
|
||||
@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