From cf20fb66e39fab4903c263b3a09c8158015a73b2 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Sun, 7 Feb 2010 22:29:31 -0600 Subject: [PATCH 1/2] adding about maps --- koans/about_maps.clj | 48 +++++++++++++++++++++++++++++++++ koans/path_to_enlightenment.clj | 3 ++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 koans/about_maps.clj diff --git a/koans/about_maps.clj b/koans/about_maps.clj new file mode 100644 index 0000000..9215453 --- /dev/null +++ b/koans/about_maps.clj @@ -0,0 +1,48 @@ +(meditations + "Maps in clojure associate keys with values" + (= __ (.size (hash-map))) + + "There are two ways to create maps" + (= {} (hash-map)) + + "A value must be supplied for each key" + (= {:a 1} (hash-map :a __)) + + "The size is the number of entries" + (= __ (.size {:a 1 :b 2})) + + "You can look up the value for a given key" + (= __ (get {:a 1 :b 2} :b)) + + "Maps can be used as lookup functions" + (= __ ({:a 1 :b 2} :a)) + + "And so can keywords" + (= __ (:a {:a 1 :b 2})) + + "But map keys need not be keywords" + (= __ ({2006 "Torino" 2010 "Vancouver" 2014 "Sochi"} 2010)) + + "You may not be able to find an entry for a key" + (= nil (get {:a 1 :b 2} :c)) + + "You can find out if a key is present" + (= __ (contains? {:a nil :b nil} :b)) + + "Or if it is missing" + (= __ (contains? {:a nil :b nil} :c)) + + "Maps are immutable, but you can create a new, 'changed' version" + (= {1 "January" 2 __} (assoc {1 "January" 2 "Febuary"} 2 "February")) + + "You can also 'remove' an entry" + (= {__ __} (dissoc {1 "January" 2 "February"} 2)) + + "Often you will need to get the keys (which will be in hash order)" + (= (list __ __ __) + (sort (keys {2006 "Torino" 2010 "Vancouver" 2014 "Sochi"}))) + + "Or the values" + (= (list "Sochi" "Torino" __) + (sort (vals {2006 "Torino" 2010 "Vancouver" 2014 "Sochi"})))) + diff --git a/koans/path_to_enlightenment.clj b/koans/path_to_enlightenment.clj index e8b9d62..e0934f7 100644 --- a/koans/path_to_enlightenment.clj +++ b/koans/path_to_enlightenment.clj @@ -15,4 +15,5 @@ (load "about_equalities") (load "about_vectors") (load "about_sets") -(println "You have acheived clojure enlightenment. Namaste.") \ No newline at end of file +(load "about_maps") +(println "You have acheived clojure enlightenment. Namaste.") From f3fd51c957ab0ab503f2a61b25bbdeefea20b8e1 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Mon, 8 Feb 2010 22:57:08 -0600 Subject: [PATCH 2/2] adding about functions --- koans/about_functions.clj | 21 +++++++++++++++++++++ koans/path_to_enlightenment.clj | 1 + 2 files changed, 22 insertions(+) create mode 100644 koans/about_functions.clj diff --git a/koans/about_functions.clj b/koans/about_functions.clj new file mode 100644 index 0000000..0c5aac3 --- /dev/null +++ b/koans/about_functions.clj @@ -0,0 +1,21 @@ +(defn count-items [& items] + (count items)) + +(meditations + "Functions are often defined before they are used" + (= __ (count-items :a :b :c :d)) + + "But they can also be defined inline" + (= __ ((fn [n] (Math/pow n 10)) 2)) + + "Or using even shorter syntax" + (= __ (#(Math/pow % 10))) + + "Functions can beget others" + (= __ (((fn [] + (fn [a b] (+ a b)))) + 4 5)) + + "Higher-order functions take function arguments" + (= 25 ((fn [f] (f 5)) + (fn [n] (__ __ __))))) diff --git a/koans/path_to_enlightenment.clj b/koans/path_to_enlightenment.clj index e0934f7..745524a 100644 --- a/koans/path_to_enlightenment.clj +++ b/koans/path_to_enlightenment.clj @@ -16,4 +16,5 @@ (load "about_vectors") (load "about_sets") (load "about_maps") +(load "about_functions") (println "You have acheived clojure enlightenment. Namaste.")