On Fri, Dec 16, 2011 at 3:48 AM, Sunil S Nandihalli
<[email protected]> wrote:
> Thanks Mark, for your response... While I understand what you are saying.. I
> don't understand as to why the following is working ..
>
> (assoc (pm/priority-map 1 1 2 1) 3 1)
>
> gives
> {1 1 2 1 3 1}
> Am I not supposed to be using this? will it eventually bite me?
>
> Sunil.

That looks fine to me.  You can certainly have multiple things
assigned to the same priority.  You can't assume, however, that the
(3,1) key-value pair will come after the other two.  Since these pairs
all have the same priority, the internal ordering is somewhat
arbitrary.  One way to think about it is that 1 2 and 3 are all put in
the same priority "bucket" because they have exactly the same assigned
priority.

The problem with your original example was the custom comparator you
chose.  You were using things like {1 2} and {2 4} as priorities, so
you've got two distinct priority buckets, and your comparator can't
determine which one of these priority buckets comes first (because the
counts of {1 2} and {2 4} are equal).  You need a comparator that
defines a clear ordering for all non-equal priorities.

One way to fix your example is to change your comparator to something
like #(compare [(count %1) (id %1)] [(count %2) (id %2)]) where id is
a function that maps everything to a unique integer.  For example,
maybe id could just be a memoized function that hands out
auto-incrementing ids.  Or how about something like #(if (= %1 %2) 0
(compare [(count %1) (str %1)] [(count %2) (str %2)]))

Like I said, this comes from an inherent limitation in Clojure's own
sorted collections.

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

Reply via email to