If you are looking for a guarantee in the documentation that this will
never happen in the current version of Clojure, and it is promised never to
change in that way in future Clojure versions, then I don't see anything in
the documentation that would give that guarantee.

If you are looking for a guarantee that the Clojure 1.6.0 implementation of
sorted-set-by will never call a comparator function with the same key as
long as no one ever tries to add a key a second time, that seems possible
to do by inspecting the code and carefully thinking about it.  I would be
surprised if the existing implementation ever compares the same key to
itself.

One could also create their own sorted set implementation that has the
"first equal key in wins" behavior and does not throw an exception if one
tries to add a second equal key, but leaves the sorted-map unchanged, but
it sounds like you want the exception if that is ever tried.

Andy





On Wed, Apr 22, 2015 at 2:39 AM, Henrik Heine <[email protected]>
wrote:

> Hi,
>
> I'd like to build a sorted map that uses a user-supplied comparator using
> sorted-map-by.
> And I'd like to make sure, that one will never add a key already contained
> in the map.
> This is what I came up with:
>
> (defn compare-not-equal [x y]
>   (let [r (compare x y)]
>     (if-not
>         (= 0 r) r
>         (throw
>          (RuntimeException. (str "Key collision: " x))))))
>
> (into (sorted-map-by compare-not-equal)  [[:foo 1] [:bar 2] [:bar 3]])
>
> Adding [:bar 3] will fail, which is what I wanted.
>
> This implementation depends on sorted-set-by *NEVER* calling the
> comparator with [x x] for what ever reason it may do that.
>
> So my question is: is there a guarantee that sorted-map-by will never do
> that?
>
> Is there a better way to do what I'm trying to do?
>
> - Henrik
>
>  --
> 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.

Reply via email to