From f58864deda9e5bdceea530b8dfb3654a7a1fbd24 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Thu, 11 Feb 2010 12:13:55 +0800 Subject: [PATCH 1/2] adding about map/filter/reduce --- koans/about_higher_order_functions.clj | 32 ++++++++++++++++++++++++++ koans/path_to_enlightenment.clj | 1 + 2 files changed, 33 insertions(+) create mode 100644 koans/about_higher_order_functions.clj diff --git a/koans/about_higher_order_functions.clj b/koans/about_higher_order_functions.clj new file mode 100644 index 0000000..fc9ea67 --- /dev/null +++ b/koans/about_higher_order_functions.clj @@ -0,0 +1,32 @@ +(meditations + "The map function relates a sequence to another" + (= [__ __ __] (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])) + + "Or use the names of existing functions" + (= __ (map nil? [:a :b nil :c :d])) + + "A filter can be strong" + (= __ (filter (fn [x] false) '(:anything :goes :here))) + + "Or very weak" + (= '(:anything :goes :here) (filter (fn [x] __) '(:anything :goes :here))) + + "Or somewhere in between" + (= [10 20 30] (filter (fn [x] __) [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]))) + + "Reducing can increase the result" + (= __ (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])) + + "Numbers are not the only things one can reduce" + (= "longest" (reduce (fn [a b] + (if (< __ __) b a)) + ["which" "word" "is" "longest"]))) diff --git a/koans/path_to_enlightenment.clj b/koans/path_to_enlightenment.clj index 67dfb52..ee2d974 100644 --- a/koans/path_to_enlightenment.clj +++ b/koans/path_to_enlightenment.clj @@ -18,4 +18,5 @@ (load "about_maps") (load "about_functions") (load "about_conditionals") +(load "about_higher_order_functions") (println "You have acheived clojure enlightenment. Namaste.") From 357b458241a297b8829b785dd44b80f9d2eafdfd Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Sun, 14 Feb 2010 08:19:13 +0800 Subject: [PATCH 2/2] adding runtime polymorphism --- koans/about_runtime_polymorphism.clj | 35 ++++++++++++++++++++++++++++ koans/path_to_enlightenment.clj | 1 + 2 files changed, 36 insertions(+) create mode 100644 koans/about_runtime_polymorphism.clj diff --git a/koans/about_runtime_polymorphism.clj b/koans/about_runtime_polymorphism.clj new file mode 100644 index 0000000..a8171ee --- /dev/null +++ b/koans/about_runtime_polymorphism.clj @@ -0,0 +1,35 @@ +(defn hello + ([] "Hello World!") + ([a] (str "Hello, you silly " a ".")) + ([a & more] (str "Hello to this group: " + (apply str + (interpose ", " (concat (list a) more))) + "!"))) + +(defmulti diet (fn [x] (:eater x))) +(defmethod diet :herbivore [a] __) +(defmethod diet :carnivore [a] __) +(defmethod diet :default [a] __) + +(meditations + "Some functions can be used in different ways - with no arguments" + (= __ (hello)) + + "With one argument" + (= __ (hello "world")) + + "Or with many arguments" + (= __ + (hello "Peter" "Paul" "Mary")) + + "Multimethods allow more complex dispatching" + (= "Bambi eats veggies." + (diet {:species "deer" :name "Bambi" :age 1 :eater :herbivore})) + + "Different methods are used depending on the dispatch function result" + (= "Simba eats animals." + (diet {:species "lion" :name "Simba" :age 1 :eater :carnivore})) + + "You may use a default method when no others match" + (= "I don't know what Rich Hickey eats." + (diet {:name "Rich Hickey"}))) diff --git a/koans/path_to_enlightenment.clj b/koans/path_to_enlightenment.clj index ee2d974..450ef35 100644 --- a/koans/path_to_enlightenment.clj +++ b/koans/path_to_enlightenment.clj @@ -19,4 +19,5 @@ (load "about_functions") (load "about_conditionals") (load "about_higher_order_functions") +(load "about_runtime_polymorphism") (println "You have acheived clojure enlightenment. Namaste.")