Let's look at the solutions for the next of Clojure Koans -Destructuring.
; destructuring
(def test-address
{:street-address "123 Test Lane"
:city "Testerville"
:state "TX"})
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1
(= __ ((fn [[a b]] (str b a))
[:foo :bar]))
(= ":bar:foo" ((fn [[a b]] (str b a))
[:foo :bar])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;2
(= (str "First comes love, "
"then comes marriage, "
"then comes Clojure with the baby carriage")
((fn [[a b c]] __)
["love" "marriage" "Clojure"]))
(= (str "First comes love, "
"then comes marriage, "
"then comes Clojure with the baby carriage")
((fn [[a b c]] (str "First comes " a ", then comes " b ", then comes " c " with the baby carriage"))
["love" "marriage" "Clojure"]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;3
(= "Rich Hickey aka The Clojurer aka Go Time aka Macro Killah"
(let [[first-name
last-name & aliases]
(list "Rich" "Hickey" "The Clojurer" "Go Time" "Macro Killah")]
__))
(= "Rich Hickey aka The Clojurer aka Go Time aka Macro Killah"
(let [[first-name
last-name & aliases]
(list "Rich" "Hickey" "The Clojurer" "Go Time" "Macro Killah")]
(apply str (interpose " aka " (cons (str first-name " " last-name) aliases)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;4
(= {:original-parts ["Steven" "Hawking"] :named-parts {:first "Steven" :last "Hawking"}}
(let [[first-name
last-name :as full-name] ["Steven" "Hawking"]]
__))
(= {:original-parts ["Steven" "Hawking"] :named-parts {:first "Steven" :last "Hawking"}}
(let [[first-name
last-name :as full-name] ["Steven" "Hawking"]]
(hash-map :original-parts full-name :named-parts (hash-map :first first-name :last last-name))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;5
(= "123 Test Lane, Testerville, TX"
(let [{street-address
:street-address, city :city, state :state} test-address]
__))
(= "123 Test Lane, Testerville, TX"
(let [{street-address
:street-address, city :city, state :state} test-address]
(apply str (interpose ", " (list street-address city state)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;6
(= "123 Test Lane, Testerville, TX"
(let [{:keys [street-address __ __]} test-address]
__))
(= "123 Test Lane, Testerville, TX"
(let [{:keys [street-address city state]} test-address]
(apply str (interpose ", " (list street-address city state)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;7
(= "Test Testerson, 123 Test Lane, Testerville, TX"
(___ ["Test" "Testerson"] test-address)))
(= "Test Testerson, 123 Test Lane, Testerville, TX"
((fn [[a b] {:keys [street-address
city state]}]
(apply str (interpose ", " (list (str a " " b) street-address city
state))))
["Test" "Testerson"] test-address))
No comments:
Post a Comment