From 8d6d9b0ecf7612ed16775dedf480c25e2339d1ca Mon Sep 17 00:00:00 2001 From: Oli Clive-Griffin Date: Thu, 3 Jun 2021 20:38:58 +1200 Subject: [PATCH] runtime_polymorphism and lazy_sequences done --- src/koans/09_higher_order_functions.clj | 20 ++++++++++---------- src/koans/10_runtime_polymorphism.clj | 12 ++++++------ src/koans/11_lazy_sequences.clj | 14 +++++++------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/koans/09_higher_order_functions.clj b/src/koans/09_higher_order_functions.clj index 8b7fd3c..7218ac5 100644 --- a/src/koans/09_higher_order_functions.clj +++ b/src/koans/09_higher_order_functions.clj @@ -3,33 +3,33 @@ (meditations "The map function relates a sequence to another" - (= [__ __ __] (map (fn [x] (* 4 x)) [1 2 3])) + (= [4 8 12] (map (fn [x] (* 4 x)) [1 2 3])) "You may create that mapping" - (= [1 4 9 16 25] (map (fn [x] __) [1 2 3 4 5])) + (= [1 4 9 16 25] (map (fn [x] (* x x)) [1 2 3 4 5])) "Or use the names of existing functions" - (= __ (map nil? [:a :b nil :c :d])) + (= [false false true false false] (map nil? [:a :b nil :c :d])) "A filter can be strong" - (= __ (filter (fn [x] false) '(:anything :goes :here))) + (= '() (filter (fn [x] false) '(:anything :goes :here))) "Or very weak" - (= __ (filter (fn [x] true) '(:anything :goes :here))) + (= '(:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here))) "Or somewhere in between" - (= [10 20 30] (filter (fn [x] __) [10 20 30 40 50 60 70 80])) + (= [10 20 30] (filter (fn [x] (< x 31)) [10 20 30 40 50 60 70 80])) "Maps and filters may be combined" - (= [10 20 30] (map (fn [x] __) (filter (fn [x] __) [1 2 3 4 5 6 7 8]))) + (= [10 20 30] (map (fn [x] (* x 10 )) (filter (fn [x] (< x 4)) [1 2 3 4 5 6 7 8]))) "Reducing can increase the result" - (= __ (reduce (fn [a b] (* a b)) [1 2 3 4])) + (= 24 (reduce (fn [a b] (* a b)) [1 2 3 4])) "You can start somewhere else" - (= 2400 (reduce (fn [a b] (* a b)) __ [1 2 3 4])) + (= 2400 (reduce (fn [a b] (* a b)) 100 [1 2 3 4])) "Numbers are not the only things one can reduce" (= "longest" (reduce (fn [a b] - (if (< __ __) b a)) + (if (< (count a) (count b)) b a)) ["which" "word" "is" "longest"]))) diff --git a/src/koans/10_runtime_polymorphism.clj b/src/koans/10_runtime_polymorphism.clj index 8be1c6a..5532322 100644 --- a/src/koans/10_runtime_polymorphism.clj +++ b/src/koans/10_runtime_polymorphism.clj @@ -10,19 +10,19 @@ "!"))) (defmulti diet (fn [x] (:eater x))) -(defmethod diet :herbivore [a] __) -(defmethod diet :carnivore [a] __) -(defmethod diet :default [a] __) +(defmethod diet :herbivore [a] (str (a :name) " eats veggies.")) +(defmethod diet :carnivore [a] (str (a :name) " eats animals.")) +(defmethod diet :default [a] (str "I don't know what " (a :name) " eats.")) (meditations "Some functions can be used in different ways - with no arguments" - (= __ (hello)) + (= "Hello World!" (hello)) "With one argument" - (= __ (hello "world")) + (= "Hello, you silly world." (hello "world")) "Or with many arguments" - (= __ + (= "Hello to this group: Peter, Paul, Mary!" (hello "Peter" "Paul" "Mary")) "Multimethods allow more complex dispatching" diff --git a/src/koans/11_lazy_sequences.clj b/src/koans/11_lazy_sequences.clj index 5144361..8a78fce 100644 --- a/src/koans/11_lazy_sequences.clj +++ b/src/koans/11_lazy_sequences.clj @@ -3,26 +3,26 @@ (meditations "There are many ways to generate a sequence" - (= __ (range 1 5)) + (= '(1 2 3 4) (range 1 5)) "The range starts at the beginning by default" - (= __ (range 5)) + (= [0 1 2 3 4] (range 5)) "Only take what you need when the sequence is large" (= [0 1 2 3 4 5 6 7 8 9] - (take __ (range 100))) + (take 10 (range 100))) "Or limit results by dropping what you don't need" (= [95 96 97 98 99] - (drop __ (range 100))) + (drop 95 (range 100))) "Iteration provides an infinite lazy sequence" - (= __ (take 8 (iterate (fn [x] (* x 2)) 1))) + (= [1 2 4 8 16 32 64 128] (take 8 (iterate (fn [x] (* x 2)) 1))) "Repetition is key" (= [:a :a :a :a :a :a :a :a :a :a] - (repeat 10 __)) + (repeat 10 :a)) "Iteration can be used for repetition" (= (repeat 100 "hello") - (take 100 (iterate ___ "hello")))) + (take 100 (iterate (fn [x] x) "hello"))))