Distinct on the input values wouldn't solve your problems, as the entries
are indeed distinct even with repeated keys.  Calling 'distinct' on the
input keys is about the same cost as building up a map, since it internally
uses a set that is backed by a map ;-), but you seem to be more concerned
about the order of overriding, expecting the first value to when, when the
behavior of zipmap and things like 'into' is 'as if by repeated conj', in
other words, last wins.  If you are concerned about keys overriding, there
is an alternative with map literals and 'apply hash-map', so you can do
your zipping without zipmap, then apply it to a hash-map call.

> (hash-map :a 1 :a 2)
java.lang.IllegalArgumentException: Duplicate key: :a

> (defn my-zipmap [s1 s2] (apply hash-map (interleave s1 s2)))
#'sandbox10223/my-zipmap
> (my-zipmap [:a :b :c] (range))
{:a 0, :c 2, :b 1}
> (my-zipmap [:a :b :a] (range))
java.lang.IllegalArgumentException: Duplicate key: :a



On Fri, Jul 29, 2016 at 12:33 PM Daniel Dinnyes <dinny...@gmail.com> wrote:

> Actually, on second thought:
>
> Doing a distinct on the input values is definitely not acceptable, both
> for performance, and for behavior as it potentially changes the order of
> the pairing of elements.
>
> OTOH, in real life zipmap would be rarely used with the intention to
> define the same key multiple times. It would be definitely a human-friendly
> addition to log some warning when a key is to be defined multiple times,
> especially if it can avoid impact on performance.
>
> Cheers,
> D.
>
>
> On Friday, 29 July 2016 17:07:14 UTC+1, Daniel Dinnyes wrote:
>>
>> Hi All,
>>
>> This post is just to mention my recent experience with one of the
>> clojure.core/zipmap, by witch I was burned badly recently. Consider the
>> following example code
>>
>> (def ks [:a :b :c :d :e :f :b :c :d])
>>
>> (def m (zipmap ks (range)))
>>
>> (get m :b)
>>
>> > 6
>>
>> Of course the "ks" in my case was like 100-and-something names, which I
>> didn't notice got concatenated twice by accident. Alright I accept, it was
>> a ridiculously stupid mistake, but finding out what went wrong was
>> definitely not straightforward (kind of questioning your sanity feeling)...
>> I had to grab a friend for some "second pair of eyes" and her immediate
>> reaction was "(= (count (distinct ks)) (count ks))", and said there you go.
>>
>> Not sure if there is anything which could be improved on the docstring,
>> or the behavior. Maybe if the docstring has put a tiny bit more emphasis on
>> the implementation details: that it's going to be a reduce over the
>> key-value pairs into an empty map, that would have triggered the "aha"
>> moment for me.
>>
>> Anyways, I am not claiming there is something to be improved / changed
>> here, especially without affecting performance. This is just "for the
>> record" / "future reference".
>>
>> Cheers,
>>
>> D.
>>
> --
> 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.

Reply via email to