It all boils down to the fact that vectors are not seqs. If you macroexpand:
user> > (macroexpand '(let [{:keys [opt1]} [:opt1 true]] [opt1]))
(let* [map__22659 [:opt1 true]
map__22659 (if (clojure.core/seq? map__22659)
(clojure.lang.PersistentHashMap/create
(clojure.core/seq map__22659)) ;; <--- here!
map__22659)
opt1 (clojure.core/get map__22659 :opt1)]
[opt1])
so if you :keys destructure something which is not a seq it will be passed
directly to clojure.core/get, otherwise it's used to create a
PersistentHashMap (notably, PersistentHashMap cannot be created from a
vector since it's not an ISeq
<https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentHashMap.java#L72>
).
Cheers,
c.
Il giorno lunedì 2 gennaio 2017 19:03:50 UTC+1, Nicola Mometto ha scritto:
>
> AFAIK treating kv lists as maps in destructuring was only introduced to
> support kwargs destructuring, hence why it's not supported for vectors.
>
> On 02/01/17 18:00, Sean Corfield wrote:
>
> This one had me scratching my head a bit too… here’s what I _*think*_ is
> going on:
>
>
>
> First off, note that the most usual way to use the :keys destructuring is
> with a map:
>
>
>
> (let [{:keys [opt1]} {:opt1 true}] [opt1]) ==> [true]
>
>
>
> As you noted, the guide explicitly calls out “lists” and in this case you
> are passing a literal list:
>
>
>
> (let [{:keys [opt1]} '(:opt1 true)] [opt1]) ==> [true]
>
>
>
> You’d also get the same answer with:
>
>
>
> (let [{:keys [opt1]} (list :opt1 true)] [opt1]) ==> [true]
>
>
>
> But in this code, you have a vector, not a list:
>
>
>
> (let [{:keys [opt1]} [:opt1 true]] [opt1]) ==> [nil]
>
>
>
> If you turn your vector into a sequence, it _*does*_ work:
>
>
>
> (let [{:keys [opt1]} (seq [:opt1 true])] [opt1]) ==>
> [true]
>
>
>
> So it allows lists and sequences but not vectors here.
>
>
>
> I’d be interested to know why vector isn’t treated the same as list /
> sequence here...?
>
>
>
> Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
> An Architect's View -- http://corfield.org/
>
> "If you're not annoying somebody, you're not really alive."
> -- Margaret Atwood
>
>
>
> On 1/2/17, 2:41 AM, "mattias w" <[email protected] <javascript:> on
> behalf of [email protected] <javascript:>> wrote:
>
>
>
> Could someone explain why the first doesn't work and the 2nd does?
>
>
>
> (let [{:keys [opt1]} [:opt1 true]] [opt1]) ==> [nil]
>
>
>
> (let [{:keys [opt1]} '(:opt1 true)] [opt1]) ==> [true]
>
>
>
> According to http://clojure.org/guides/destructuring
>
>
>
> "Associative destructuring also works with lists of key-value pairs for
> keyword-arg parsing."
>
>
>
> If I read the definition literally, I see, it says "lists" and not
> "sequences", so the behaviour is correct, so maybe a better question is why
> the definitions isn't
>
>
>
> "Associative destructuring also works with sequences of key-value pairs
> for keyword-arg parsing."
>
>
>
> It this specific case, when you destructor the args, it looks like a
> vector, but internally it is a list, so it works.
>
>
>
> Happy New Year!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected] <javascript:>
> 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] <javascript:>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected] <javascript:>
> 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] <javascript:>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.