clojure-koans/src/koans/05_maps.clj
Laura Brown f60275e6e4 Update 05_maps.clj
"Often you will need to get the keys, but the order is undependable"
The keys value pairs are already ordered by key. Why not make the original map un-ordered to force the user to think about sorting them?

"You can get the values in a similar way"
Two of the three values are already provided in the answer, the answer to this challenge can be arrived at by providing the missing one through process of elimination without thinking about sorting.
2013-09-18 08:45:07 -05:00

48 lines
1.4 KiB
Clojure

(meditations
"Don't get lost when creating a map"
(= {:a 1 :b 2} (hash-map :a 1 __ __))
"A value must be supplied for each key"
(= {:a 1} (hash-map :a __))
"The size is the number of entries"
(= __ (count {: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 functions to do lookups"
(= __ ({: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"
(= __ (get {:a 1 :b 2} :c))
"But you can provide your own default"
(= __ (get {:a 1 :b 2} :c :key-not-found))
"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 and improved version"
(= {1 "January" 2 __} (assoc {1 "January" } 2 "February"))
"You can also create a new version with an entry removed"
(= {__ __} (dissoc {1 "January" 2 "February"} 2))
"Often you will need to get the keys, but the order is undependable"
(= (list __ __ __)
(sort (keys {2010 "Vancouver" 2014 "Sochi" 2006 "Torino"})))
"You can get the values in a similar way"
(= (list __ __ __)
(sort (vals {2006 "Torino" 2010 "Vancouver" 2014 "Sochi"}))))