Hi,

you are using comparator incorrectly. The function you pass there should 
return true, when x is to the left of y when called as (f x y). See the 
following example.

user=> (defrecord Foo [a b])
user.Foo
; Wrong usage: your example (The new element is always smaller!)
user=> (-> (sorted-set-by (comparator (juxt :a :b))) (conj (Foo. 1 2)) 
(conj (Foo. 1 3)))
#{#user.Foo{:a 1, :b 3} #user.Foo{:a 1, :b 2}}
user=> (-> (sorted-set-by (comparator (juxt :a :b))) (conj (Foo. 1 2)) 
(conj (Foo. 1 1)))
#{#user.Foo{:a 1, :b 1} #user.Foo{:a 1, :b 2}}
user=> (-> (sorted-set-by (comparator (juxt :a :b))) (conj (Foo. 1 2)) 
(conj (Foo. 1 2)))
#{#user.Foo{:a 1, :b 2} #user.Foo{:a 1, :b 2}}
; Correct usage: lexicographic ordering
user=> (-> (sorted-set-by (comparator #(or (< (:a %1) (:a %2)) (< (:b %1) 
(:b %2))))) (conj (Foo. 1 2)) (conj (Foo. 1 1)))
#{#user.Foo{:a 1, :b 1} #user.Foo{:a 1, :b 2}}
user=> (-> (sorted-set-by (comparator #(or (< (:a %1) (:a %2)) (< (:b %1) 
(:b %2))))) (conj (Foo. 1 2)) (conj (Foo. 1 3)))
#{#user.Foo{:a 1, :b 2} #user.Foo{:a 1, :b 3}}
user=> (-> (sorted-set-by (comparator #(or (< (:a %1) (:a %2)) (< (:b %1) 
(:b %2))))) (conj (Foo. 1 2)) (conj (Foo. 1 2)))
#{#user.Foo{:a 1, :b 2}}

HTH.

Meikel

-- 
-- 
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/groups/opt_out.


Reply via email to