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] on behalf of
[email protected]> 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]
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.
--
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.