Re: Destructing lists using -> Difference between '(:opt1 true) and [:opt1 true]

2017-01-03 Thread skuro
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 

).

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"  behalf of matt...@gmail.com > 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 clo...@googlegroups.com 
> Note that posts from new members are moderated - please be patient with 
> your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com 
> 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 clojure+u...@googlegroups.com .
> 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 clo...@googlegroups.com 
> Note that posts from new members are moderated - please be patient with 
> your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com 
> 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 clojure+u...@googlegroups.com .
> 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to

Re: Destructing lists using -> Difference between '(:opt1 true) and [:opt1 true]

2017-01-02 Thread Nicola Mometto
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"  on behalf of matti...@gmail.com 
> 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient 
with your first post.

To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com 
.

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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient 
with your first post.

To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com 
.

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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Destructing lists using -> Difference between '(:opt1 true) and [:opt1 true]

2017-01-02 Thread Sean Corfield
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"  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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Destructing lists using -> Difference between '(:opt1 true) and [:opt1 true]

2017-01-02 Thread mattias w
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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.