Actually, sorted-map-by does behave the same way, but in your example you tried to lookup a value instead of a key:
user> (def m (sorted-map-by #(< (%1 0) (%2 0)) [1 :a] [2 :b])) #'user/m user> (get m [1 :foo]) [2 :b] It looks like PersistentTreeMap.entryAt<https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentTreeMap.java#L285>is responsible for this behaviour. Regards, Stuart On 13 March 2012 05:20, mnicky <[email protected]> wrote: > It seems that when using the sorted set with my own comparator > (sorted-set-by), > the lookup via 'contains?' function is based only on the part of the items > that participate in the ordering: > > (contains? (sorted-set [1 :a] [2 :b]) [2 :c]) > ;=> false > > (contains? (sorted-set-by #(< (%1 0) (%2 0)) [1 :a] [2 :b]) [2 :c]) > ;=> true > > The documentation of 'sorted-set-by' says that the _whole_ items are keys: > > (doc sorted-set-by) > ; clojure.core/sorted-set-by > ; ([comparator & keys]) > ; Returns a new sorted set with supplied keys, using the supplied > comparator. > ; nil > > So according to the documentation of 'contains?', it should do lookup > based on the whole items, not just their parts used in the comparator: > > (doc contains?) > ; clojure.core/contains? > ; ([coll key]) > ; Returns true if key is present in the given collection, otherwise > ; returns false. Note that for numerically indexed collections like > ; vectors and Java arrays, this tests if the numeric key is within the > ; range of indexes. 'contains?' operates constant or logarithmic time; > ; it will not perform a linear search for a value. See also 'some'. > ; nil > > > It's also worth noting that 'sorted-map-by' doesn't behave in this way: > > (contains? (sorted-map-by #(< (%1 0) (%2 0)) [1 :a] [2 :b]) [2 :c]) > ;=> false > > Can this be a bug? If not, what's the reason behind this behavior? > > > -- Mnicky > > -- > 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 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
