diff --git a/ideaboard.txt b/ideaboard.txt index b30f6d5..7fa88b0 100644 --- a/ideaboard.txt +++ b/ideaboard.txt @@ -1,7 +1,6 @@ Concepts / Language Features ===== new record syntax -Agents Vars state identity lifetime Metadata diff --git a/resources/koans.clj b/resources/koans.clj index cb1a8a8..eefe531 100644 --- a/resources/koans.clj +++ b/resources/koans.clj @@ -203,4 +203,9 @@ 5 :hello (6 :this :are) - ]}]] + ]}] + ["21_agents" {"__" [true + 10 + 10 + 12 + 12]}]] diff --git a/src/koans/21_agents.clj b/src/koans/21_agents.clj new file mode 100644 index 0000000..816c53d --- /dev/null +++ b/src/koans/21_agents.clj @@ -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)))