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.

Reply via email to