From 7dd48a41ac0f0e6ece932b277076d855a0614cd3 Mon Sep 17 00:00:00 2001 From: Ignacy Moryc Date: Sat, 4 Aug 2012 12:11:33 +0200 Subject: [PATCH 1/5] Add koans for partition function --- ideaboard.txt | 1 - resources/koans.clj | 9 ++++++++- src/koans/20_partition.clj | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/koans/20_partition.clj diff --git a/ideaboard.txt b/ideaboard.txt index 7e1f71d..710bac9 100644 --- a/ideaboard.txt +++ b/ideaboard.txt @@ -15,7 +15,6 @@ Particular Functions fnil - creating_a_function juxt - creating_a_function constantly - creating_a_function -partition flatten frequencies diff --git a/resources/koans.clj b/resources/koans.clj index 41ce830..cb1a8a8 100644 --- a/resources/koans.clj +++ b/resources/koans.clj @@ -196,4 +196,11 @@ ] "___" [#(.toUpperCase %) ] - }]] + }] + ["20_partition" {"__" [partition + [:a :b :c] + '((0 1 2) (3 4)) + 5 + :hello + (6 :this :are) + ]}]] diff --git a/src/koans/20_partition.clj b/src/koans/20_partition.clj new file mode 100644 index 0000000..1d21dd0 --- /dev/null +++ b/src/koans/20_partition.clj @@ -0,0 +1,18 @@ +(meditations + "To split a collection you can use the partition function" + (= '((0 1) (2 3)) (__ 2 (range 4))) + + "But watch out if there is not enough elements to form n sequences" + (= '(__) (partition 3 [:a :b :c :d :e])) + + "You can use partition-all to also get partitions with less then n elements" + (= __ (partition-all 3 (range 5))) + + "If you need to, you can start each sequence with an offset" + (= '((0 1 2) (5 6 7) (10 11 12)) (partition 3 __ (range 13))) + + "Consider padding the last sequence with some default values.." + (= '((0 1 2) (3 4 5) (6 :hello)) (partition 3 3 [__] (range 7))) + + ".. but notice that they will only pad up to given sequence length" + (= '((0 1 2) (3 4 5) __) (partition 3 3 [:this :are "my" "words"] (range 7)))) From ffdd7cc600c683d1d02b5425c8895d35fcdad863 Mon Sep 17 00:00:00 2001 From: Ignacy Moryc Date: Sun, 23 Sep 2012 13:55:03 +0200 Subject: [PATCH 2/5] 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)))) From 00385eee11b1b004798e5cbd81bcfe3ea3de1754 Mon Sep 17 00:00:00 2001 From: Ignacy Moryc Date: Sun, 23 Sep 2012 14:01:28 +0200 Subject: [PATCH 3/5] Fixed assertions and comments --- ideaboard.txt | 1 - resources/koans.clj | 2 +- src/koans/21_agents.clj | 18 +++++++++--------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/ideaboard.txt b/ideaboard.txt index 710bac9..850e43e 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 c461f60..eefe531 100644 --- a/resources/koans.clj +++ b/resources/koans.clj @@ -208,4 +208,4 @@ 10 10 12 - IllegalStateException]}]] + 12]}]] diff --git a/src/koans/21_agents.clj b/src/koans/21_agents.clj index 1806f2d..4b02fd1 100644 --- a/src/koans/21_agents.clj +++ b/src/koans/21_agents.clj @@ -9,23 +9,23 @@ "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, + "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, of that action that gets assigned to agent's state" (= __ (do (send agent-example + 2) @agent-example)) + "You can't just send a value to agent" + (= __ (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-error agent-example)))) + @agent-example))) From c8f711e209a8987c80920ba07d392ab0f878d40a Mon Sep 17 00:00:00 2001 From: Ignacy Moryc Date: Sun, 23 Sep 2012 15:38:31 +0200 Subject: [PATCH 4/5] Use let scope to bing the agent --- src/koans/21_agents.clj | 49 +++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/koans/21_agents.clj b/src/koans/21_agents.clj index 4b02fd1..2b70c70 100644 --- a/src/koans/21_agents.clj +++ b/src/koans/21_agents.clj @@ -1,31 +1,32 @@ -(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)) + (let [agent-example (agent 10)] - "To get agent's value you dereference it" - (= __ @agent-example) + "Creating an Agent is as simple as assigning it a value" + (= __ (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, of that action that gets assigned to agent's state" - (= __ (do - (send agent-example + 2) - @agent-example)) + (= __ (do + (send agent-example + 2) + @agent-example)) - "You can't just send a value to agent" - (= __ (do - (set-error-mode! agent-example :continue) - (send agent-example 20) - @agent-example)) + "You can't just send a value to agent" + (= __ (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))) + "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))) From 7fecd24dd6bf96868eab27df34c617c3d85388b1 Mon Sep 17 00:00:00 2001 From: Ignacy Moryc Date: Sun, 23 Sep 2012 15:49:00 +0200 Subject: [PATCH 5/5] Updated comments --- src/koans/21_agents.clj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/koans/21_agents.clj b/src/koans/21_agents.clj index 2b70c70..816c53d 100644 --- a/src/koans/21_agents.clj +++ b/src/koans/21_agents.clj @@ -1,20 +1,20 @@ (meditations (let [agent-example (agent 10)] - "Creating an Agent is as simple as assigning it a value" + "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 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, - of that action that gets assigned to agent's state" + "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" + "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)