done destructuring
This commit is contained in:
parent
c1e56f5c29
commit
57a69bdc38
1 changed files with 17 additions and 8 deletions
|
|
@ -8,37 +8,46 @@
|
||||||
|
|
||||||
(meditations
|
(meditations
|
||||||
"Destructuring is an arbiter: it breaks up arguments"
|
"Destructuring is an arbiter: it breaks up arguments"
|
||||||
(= __ ((fn [[a b]] (str b a))
|
(= ":bar:foo" ((fn [[a b]] (str b a))
|
||||||
[:foo :bar]))
|
[:foo :bar]))
|
||||||
|
|
||||||
"Whether in function definitions"
|
"Whether in function definitions"
|
||||||
(= (str "An Oxford comma list of apples, "
|
(= (str "An Oxford comma list of apples, "
|
||||||
"oranges, "
|
"oranges, "
|
||||||
"and pears.")
|
"and pears.")
|
||||||
((fn [[a b c]] __)
|
((fn [[a b c]] (str "An Oxford comma list of "
|
||||||
|
a ", "
|
||||||
|
b ", and "
|
||||||
|
c "."))
|
||||||
["apples" "oranges" "pears"]))
|
["apples" "oranges" "pears"]))
|
||||||
|
|
||||||
"Or in let expressions"
|
"Or in let expressions"
|
||||||
(= "Rich Hickey aka The Clojurer aka Go Time aka Lambda Guru"
|
(= "Rich Hickey aka The Clojurer aka Go Time aka Lambda Guru"
|
||||||
(let [[first-name last-name & aliases]
|
(let [[first-name last-name & aliases]
|
||||||
(list "Rich" "Hickey" "The Clojurer" "Go Time" "Lambda Guru")]
|
(list "Rich" "Hickey" "The Clojurer" "Go Time" "Lambda Guru")]
|
||||||
__))
|
(str first-name " " last-name (clojure.string/join (for [alias aliases]
|
||||||
|
(str " aka " alias))))))
|
||||||
|
|
||||||
"You can regain the full argument if you like arguing"
|
"You can regain the full argument if you like arguing"
|
||||||
(= {:original-parts ["Stephen" "Hawking"] :named-parts {:first "Stephen" :last "Hawking"}}
|
(= {:original-parts ["Stephen" "Hawking"] :named-parts {:first "Stephen" :last "Hawking"}}
|
||||||
(let [[first-name last-name :as full-name] ["Stephen" "Hawking"]]
|
(let [[first-name last-name :as full-name] ["Stephen" "Hawking"]]
|
||||||
__))
|
{:original-parts full-name :named-parts {:first first-name :last last-name}}))
|
||||||
|
|
||||||
"Break up maps by key"
|
"Break up maps by key"
|
||||||
(= "123 Test Lane, Testerville, TX"
|
(= "123 Test Lane, Testerville, TX"
|
||||||
(let [{street-address :street-address, city :city, state :state} test-address]
|
(let [{street-address :street-address, city :city, state :state} test-address]
|
||||||
__))
|
(clojure.string/join ", " [street-address city state])))
|
||||||
|
|
||||||
"Or more succinctly"
|
"Or more succinctly"
|
||||||
(= "123 Test Lane, Testerville, TX"
|
(= "123 Test Lane, Testerville, TX"
|
||||||
(let [{:keys [street-address __ __]} test-address]
|
(let [{:keys [street-address city state]} test-address]
|
||||||
__))
|
(clojure.string/join ", " [street-address city state])))
|
||||||
|
|
||||||
"All together now!"
|
"All together now!"
|
||||||
(= "Test Testerson, 123 Test Lane, Testerville, TX"
|
(= "Test Testerson, 123 Test Lane, Testerville, TX"
|
||||||
(___ ["Test" "Testerson"] test-address)))
|
((fn [[a b]
|
||||||
|
{:keys [street-address city state]}]
|
||||||
|
(str a " " b ", " street-address ", " city ", " state)
|
||||||
|
)
|
||||||
|
["Test" "Testerson"]
|
||||||
|
test-address)))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue