On Wed, Mar 25, 2009 at 7:21 AM, hoeck <[email protected]> wrote:
>
> there is already a filed issue and a patch from Timothy Pratley which
> adds sorted-set-by to clojure:
> http://code.google.com/p/clojure/issues/detail?id=76&colspec=ID%20Type%20Status%20Priority%20Reporter%20Owner%20Summary
>
> But its priority is set tow "low".
Until the patch is included, you could use this:
(import '(clojure.lang PersistentTreeMap APersistentSet Reversible Sorted))
(defn sorted-set-by [cmp & items]
((fn mkset [m #^PersistentTreeMap impl]
(proxy [APersistentSet Reversible Sorted] [m impl]
(disjoin [k] (if (contains? impl k) (mkset m (dissoc impl k)) this))
(cons [k] (if (contains? impl k) this (mkset m (assoc impl k k))))
(empty [] (mkset m (empty impl)))
(rseq [] (.seq this false))
(withMeta [m2] (mkset m2 impl))
(comparator [] (.comparator impl))
(entryKey [e] e)
(seq ([] (keys impl))
([asc] (keys (.seq impl asc))))
(seqFrom [k asc] (keys (.seqFrom impl k asc)))))
nil (apply sorted-map-by cmp (interleave items items))))
user=> (sorted-set-by > 1 2 3 4 5)
#{5 4 3 2 1}
--Chouser
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---