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
"Destructuring is an arbiter: it breaks up arguments"
(= __ ((fn [[a b]] (str b a))
@ -21,4 +26,17 @@
(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))
(repeat " "))))
'{: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\""
"\"hello\""
"\"better\""
"\"better!!!\""
'(dosync (ref-set the-world 0))
]
"___" ['(comp exclamator exclamator exclamator)
'(fn [x] (+ 20 x))]}