From ffdd7cc600c683d1d02b5425c8895d35fcdad863 Mon Sep 17 00:00:00 2001 From: Ignacy Moryc Date: Sun, 23 Sep 2012 13:55:03 +0200 Subject: [PATCH] Koan for agents --- resources/koans.clj | 7 ++++++- src/koans/21_agents.clj | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/koans/21_agents.clj diff --git a/resources/koans.clj b/resources/koans.clj index cb1a8a8..c461f60 100644 --- a/resources/koans.clj +++ b/resources/koans.clj @@ -203,4 +203,9 @@ 5 :hello (6 :this :are) - ]}]] + ]}] + ["21_agents" {"__" [true + 10 + 10 + 12 + IllegalStateException]}]] diff --git a/src/koans/21_agents.clj b/src/koans/21_agents.clj new file mode 100644 index 0000000..1806f2d --- /dev/null +++ b/src/koans/21_agents.clj @@ -0,0 +1,31 @@ +(def agent-example + "Agents provide shared access to mutable state." + (agent 10)) + +(meditations + "Creating an Agent is as simple as assigning it a value" + (= __ (instance? clojure.lang.Agent agent-example)) + + "To get agent's value you dereference it" + (= __ @agent-example) + + "To set a new value, you use send or send-all functions, but you can't just send anything.." + (= __ (do + (set-error-mode! agent-example :continue) + (send agent-example 20) + @agent-example)) + + "Send takes a function and arguments and sends them to the agent. The 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" + (= __ (do + (send agent-example + 2) + @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-error agent-example))))