inconsistent behavior with destructuring ...

50 views
Skip to first unread message

Sunil S Nandihalli

unread,
Apr 19, 2012, 2:01:02 AM4/19/12
to clo...@googlegroups.com
Hi Everybody,
 I was just wondering if the following behaviour is the right behaviour.. can somebody comment?

user>> (let [{{a :a b :b :as w} :c a1 :a b1 :b :as w} {:a 10 :b 20 :c {:a 30 :b 40}}] {:a a :b b  :b1 b1 :a1 a1})    
{:a 30, :b 40, :b1 40, :a1 30}

user>> (let [{{a :a b :b :as w} :c a1 :a b1 :b :as w1} {:a 10 :b 20 :c {:a 30 :b 40}}] {:a a :b b  :b1 b1 :a1 a1})
{:a 30, :b 40, :b1 20, :a1 10}


I agree that I have two- 'w' s in the first one while I have 'w' and 'w1' in the second . And the second behaviour is the expected behaviour.. clojure-version 1.2.1

Thanks,
Sunil.

Sunil S Nandihalli

unread,
Apr 19, 2012, 2:14:01 AM4/19/12
to clo...@googlegroups.com
the behaviour seems to be the same in 1.4.0

Sunil.

Meikel Brandmeyer (kotarak)

unread,
Apr 19, 2012, 2:23:58 AM4/19/12
to clo...@googlegroups.com
Hi,

I think this is a bug. This is the expansion without any :as clause:

(let*
  [map__4496
   {:a 10, :b 20, :c {:a 30, :b 40}}
   map__4496
   (if (clojure.core/seq? map__4496)
     (clojure.core/apply clojure.core/hash-map map__4496)
     map__4496)
   map__4497
   (clojure.core/get map__4496 :c)
   map__4497
   (if (clojure.core/seq? map__4497)
     (clojure.core/apply clojure.core/hash-map map__4497)
     map__4497)
   a
   (clojure.core/get map__4497 :a)
   b
   (clojure.core/get map__4497 :b)
   a1
   (clojure.core/get map__4496 :a)
   b1
   (clojure.core/get map__4496 :b)]
  {:a a, :b b, :b1 b1, :a1 a1})

Now with the :as clauses:

(let*
  [w
   {:a 10, :b 20, :c {:a 30, :b 40}}
   w
   (if (clojure.core/seq? w)
     (clojure.core/apply clojure.core/hash-map w)
     w)
   w
   (clojure.core/get w :c)
   w
   (if (clojure.core/seq? w)
     (clojure.core/apply clojure.core/hash-map w)
     w)
   a
   (clojure.core/get w :a)
   b
   (clojure.core/get w :b)
   a1
   (clojure.core/get w :a)
   b1
   (clojure.core/get w :b)]
  {:a a, :b b, :b1 b1, :a1 a1})

As you can see :as simply replace the generated symbol with the given one. This is a mistake however. I think the correct expansion should look something like this:

(let*
  [map__4502
   {:a 10, :b 20, :c {:a 30, :b 40}}
   map__4502
   (if (clojure.core/seq? map__4502)
     (clojure.core/apply clojure.core/hash-map map__4502)
     map__4502)
   w map__4502
   map__4503
   (clojure.core/get map__4502 :c)
   map__4503
   (if (clojure.core/seq? map__4503)
     (clojure.core/apply clojure.core/hash-map map__4503)
     map__4503)
   w map__4503
   a
   (clojure.core/get map__4503 :a)
   b
   (clojure.core/get map__4503 :b)
   a1
   (clojure.core/get map__4502 :a)
   b1
   (clojure.core/get map__4502 :b)]
  {:a a, :b b, :b1 b1, :a1 a1})

Kind regards
Meikel

Sunil S Nandihalli

unread,
Apr 19, 2012, 2:28:25 AM4/19/12
to clo...@googlegroups.com
You think I should file a bug-report?
Sunil.


Meikel

--
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

Meikel Brandmeyer (kotarak)

unread,
Apr 19, 2012, 2:35:34 AM4/19/12
to clo...@googlegroups.com
Hi,

yes, I think so. The destructuring should not depend on the presence or absence of a (for a given key) unrelated option. So no matter what I do with :as the :a, :b, etc. keys should be correctly destructured, even in nested maps as in your example.

Kind regards
Meikel

Reply all
Reply to author
Forward
0 new messages