-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14_destructuring.clj
43 lines (36 loc) · 1.78 KB
/
14_destructuring.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(def test-address
{:street-address "123 Test Lane"
:city "Testerville"
:state "TX"})
(meditations
"Destructuring is an arbiter: it breaks up arguments"
(= ":bar:foo" ((fn [[a b]] (str b a))
[:foo :bar]))
"Whether in function definitions"
(= (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"]))
"Or in let expressions"
(= "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)))))
"You can regain the full argument if you like arguing"
(= {: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))))
"Break up maps by key"
(= "123 Test Lane, Testerville, TX"
(let [{street-address :street-address, city :city, state :state} test-address]
(apply str (interpose ", " (list street-address city state)))))
"Or more succinctly"
(= "123 Test Lane, Testerville, TX"
(let [{:keys [street-address city state]} test-address]
(apply str (interpose ", " (list street-address city state)))))
"All together now!"
(= "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)))