sorted-set-by?

8 views
Skip to first unread message

R. P. Dillon

unread,
Feb 3, 2009, 6:31:15 PM2/3/09
to Clojure
I want to create a sorted-set of structmaps that are sorted on a
particular key of the structmap. The set was going to act as a queue,
and initially I implemented this using a PriorityQueue with a
Comparator I made using proxy. In general, I'd prefer to use pure
Clojure, and sorted-set seemed like the right approach, but it seems
that elements in sorted set must implement Comparable, since there
doesn't seem to be a way to specify a Comparator as there is with a
sorted-map (using sorted-map-by). I couldn't find a sorted-set-by.

One idea I had was to attempt to create a subclass (using proxy) of
PersistentStructMap that implemented Comparable and use those as my
elements of the sorted set, but that seems too OO for my needs, and it
was looking kind of ugly as I started to try and write it.

I'm hoping there is some obvious idiom I'm missing here. Can anyone
enlighten me, or should I be writing my own sorted-set-by function?

Thanks,
Rick

R. P. Dillon

unread,
Feb 3, 2009, 7:05:24 PM2/3/09
to Clojure
On further reflection, perhaps the best approach would use sorted-map:

(defstruct example :msg :order)

(def a (struct "there" 2))
(def b (struct "hi" 1))
(def c (struct "everyone" 3))

We want our map to be sorted on the :order key. So:

(sorted-map (:order a) a (:order b) b (:order c) c)

I could wrap up some of this behavior into some functions that would
clean it up. Is this the best approach, or should I be approaching
this from another direction?

Timothy Pratley

unread,
Feb 3, 2009, 7:52:26 PM2/3/09
to Clojure
> On further reflection, perhaps the best approach would use sorted-map:

Just curious, does the key need to be in the struct? (seeing you'll
get key-value pairs anyhow if you use first/last etc - the info will
still be there)
If you do need the key in both places, perhaps something like this
http://groups.google.com/group/clojure/web/sorted-map-map.clj

Regards,
Tim.

R. P. Dillon

unread,
Feb 3, 2009, 8:43:01 PM2/3/09
to Clojure
> Just curious, does the key need to be in the struct? (seeing you'll
> get key-value pairs anyhow if you use first/last etc - the info will
> still be there)

Excellent point! Given that I'll be using a sorted-map now, I don't
even need the structmap!

Thanks for the code...I like what you did with the interleave in
sorted-map-map. I expect I'll make use of it as soon as I'm working
with sorting structmaps of more than two keys.

Thanks,
Rick

puzzler

unread,
Feb 7, 2009, 3:58:18 AM2/7/09
to Clojure
Still, I think it's a good point that since Clojure has sorted-map-by,
it seems logical to expect that it would also have sorted-set-by.

Rich Hickey

unread,
Feb 7, 2009, 8:27:50 AM2/7/09
to clo...@googlegroups.com

On Feb 7, 2009, at 3:58 AM, puzzler wrote:

>
> Still, I think it's a good point that since Clojure has sorted-map-by,
> it seems logical to expect that it would also have sorted-set-by.
>

Yes, this is just an API gap. Issue/patch welcome.

Rich

Timothy Pratley

unread,
Feb 11, 2009, 6:00:55 AM2/11/09
to Clojure
Added a patch as issue 76 http://code.google.com/p/clojure/issues/detail?id=76

user=> (sorted-set-by #(> (:hat %1) (:hat %2)) {:hat 2} {:hat 3} {:hat
1})
#{{:hat 3} {:hat 2} {:hat 1}}
Reply all
Reply to author
Forward
0 new messages