Add map destructuring.

This commit is contained in:
Colin Jones 2010-11-11 18:16:55 -06:00
parent a7f7df6f01
commit 3bba4879db
2 changed files with 27 additions and 2 deletions

View File

@ -1,3 +1,8 @@
(def test-address
{:street-address "123 Test Lane"
:city "Testerville"
:state "TX"})
(meditations (meditations
"Destructuring is an arbiter: it breaks up arguments" "Destructuring is an arbiter: it breaks up arguments"
(= __ ((fn [[a b]] (str b a)) (= __ ((fn [[a b]] (str b a))
@ -21,4 +26,17 @@
(let [[first-name last-name :as full-name] ["Steven" "Hawking"]] (let [[first-name last-name :as full-name] ["Steven" "Hawking"]]
__)) __))
"Break up maps by key"
(= "123 Test Lane, Testerville, TX"
(let [{street-address :street-address, city :city, state :state} test-address]
__))
"Or more succinctly"
(= "123 Test Lane, Testerville, TX"
(let [{:keys [street-address __ __]} test-address]
__))
"All together now!"
(= "Test Testerson, 123 Test Lane, Testerville, TX"
(___ ["Test" "Testerson"] test-address))
) )

View File

@ -110,13 +110,20 @@
(interleave (repeat "aka") aliases)) (interleave (repeat "aka") aliases))
(repeat " ")))) (repeat " "))))
'{:original-parts full-name '{:original-parts full-name
:named-parts {:first first-name :last last-name}} ]} :named-parts {:first first-name :last last-name}}
'(str street-address ", " city ", " state)
'city 'state
'(str street-address ", " city ", " state)]
"___" ['(fn [[fname lname]
{:keys [street-address city state]}]
(str fname " " lname ", "
street-address ", " city ", " state))
]}
"refs" {"__" ["\"hello\"" "refs" {"__" ["\"hello\""
"\"hello\"" "\"hello\""
"\"better\"" "\"better\""
"\"better!!!\"" "\"better!!!\""
'(dosync (ref-set the-world 0)) '(dosync (ref-set the-world 0))
] ]
"___" ['(comp exclamator exclamator exclamator) "___" ['(comp exclamator exclamator exclamator)
'(fn [x] (+ 20 x))]} '(fn [x] (+ 20 x))]}