2014-05-02 21:37:11 +00:00
|
|
|
(ns koans.14-destructuring
|
|
|
|
(:require [koan-engine.core :refer :all]))
|
2014-01-26 00:04:22 +00:00
|
|
|
|
2010-11-12 00:16:55 +00:00
|
|
|
(def test-address
|
|
|
|
{:street-address "123 Test Lane"
|
|
|
|
:city "Testerville"
|
|
|
|
:state "TX"})
|
|
|
|
|
2010-11-06 22:38:31 +00:00
|
|
|
(meditations
|
2010-11-08 23:27:18 +00:00
|
|
|
"Destructuring is an arbiter: it breaks up arguments"
|
2010-11-06 22:38:31 +00:00
|
|
|
(= __ ((fn [[a b]] (str b a))
|
2011-10-26 02:55:54 +00:00
|
|
|
[:foo :bar]))
|
2010-11-08 23:27:18 +00:00
|
|
|
|
|
|
|
"Whether in function definitions"
|
2015-06-24 19:11:00 +00:00
|
|
|
(= (str "An Oxford comma list of apples, "
|
|
|
|
"oranges, "
|
|
|
|
"and pears.")
|
2010-11-08 23:27:18 +00:00
|
|
|
((fn [[a b c]] __)
|
2015-06-24 19:11:00 +00:00
|
|
|
["apples" "oranges" "pears"]))
|
2010-11-08 23:27:18 +00:00
|
|
|
|
2010-11-09 00:07:59 +00:00
|
|
|
"Or in let expressions"
|
2015-06-24 19:11:00 +00:00
|
|
|
(= "Rich Hickey aka The Clojurer aka Go Time aka Lambda Guru"
|
2010-11-09 00:07:59 +00:00
|
|
|
(let [[first-name last-name & aliases]
|
2015-06-24 19:11:00 +00:00
|
|
|
(list "Rich" "Hickey" "The Clojurer" "Go Time" "Lambda Guru")]
|
2010-11-09 00:07:59 +00:00
|
|
|
__))
|
|
|
|
|
|
|
|
"You can regain the full argument if you like arguing"
|
2014-01-27 09:52:03 +00:00
|
|
|
(= {:original-parts ["Stephen" "Hawking"] :named-parts {:first "Stephen" :last "Hawking"}}
|
|
|
|
(let [[first-name last-name :as full-name] ["Stephen" "Hawking"]]
|
2010-11-09 00:07:59 +00:00
|
|
|
__))
|
|
|
|
|
2010-11-12 00:16:55 +00:00
|
|
|
"Break up maps by key"
|
|
|
|
(= "123 Test Lane, Testerville, TX"
|
|
|
|
(let [{street-address :street-address, city :city, state :state} test-address]
|
2011-10-26 02:55:54 +00:00
|
|
|
__))
|
2010-11-12 00:16:55 +00:00
|
|
|
|
|
|
|
"Or more succinctly"
|
|
|
|
(= "123 Test Lane, Testerville, TX"
|
|
|
|
(let [{:keys [street-address __ __]} test-address]
|
2011-10-26 02:55:54 +00:00
|
|
|
__))
|
2010-11-12 00:16:55 +00:00
|
|
|
|
|
|
|
"All together now!"
|
|
|
|
(= "Test Testerson, 123 Test Lane, Testerville, TX"
|
2011-10-26 02:55:54 +00:00
|
|
|
(___ ["Test" "Testerson"] test-address)))
|