Vector-as-map destructuring makes sense when you consider that vectors are
associative: they map index to value.
(let [{a 1 b 3 :as c} [:a 1 :b 2]] [a b c])
=> [1 2 [:a 1 :b 2]]
Justin
On Tuesday, August 28, 2012 8:30:58 AM UTC-4, Douglas Orr wrote:
>
> One possibly confusing titbit I came across recently relates to how
> Clojure handles destructuring of variable-length argument lists. The
> essence is that the destructuring form can appear to influence the 'shape'
> of the collection of arguments. Here is what I mean:
>
> take nothing off the head of the vector, then put all remaining elements
> into a vector c:
>
> user=> (let [[& [:as c]] [:one "one" :two "two"]] (seq c))
> (:one "one" :two "two")
>
>
> take nothing off the head of the vector, then put all remaining elements
> into a map c:
>
> user=> (let [[& {:as c}] [:one "one" :two "two"]] (seq c))
> ([:one "one"] [:two "two"])
>
>
> Notice how the structure of 'c' has changed due to the destructuring form,
> even though in both cases it refers to the whole collection. Digging down
> into this a bit, it looks like this depends on the type of the collection
> being destructured:
>
> user=> (let [{:as c} [:one "one" :two "two"]] (seq c))
> (:one "one" :two "two")
>
> user=> (let [{:as c} (seq [:one "one" :two "two"])] (seq c))
> ([:one "one"] [:two "two"])
>
>
> I can see why this is necessary for multiple-args destructuring to work,
> but I think my small mind finds this 'automatic marshalling' of
> lists-or-seqs into maps a bit confusing. Maybe my only real problem is the
> destructuring of vectors as maps, which somewhat 'works' in the above
> example, but isn't really consistent. Any thoughts?
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en