Hi, I want to sort a set/map according to an ordering given by a seq of elements - e.g.
(def some-order [:u :a :e :i :o]) (def some-order-fn (order-fn some-order)) (sorted-set-by some-order-fn :a :e :i :o :u) ; --> #{:u :a :e :i :o} This is what I came up with: (defn order-fn [ks] #(- (.indexOf ks %1) (.indexOf ks %2))) But then one may want to replace .indexOf for some other ord-function- like this: (defn order-fn ([ks] (order-fn ks #(.indexOf %1 %2))) ([ks ord-fn] #(- (ord-fn ks %1) (ord-fn ks %2)))) Now we may force the elements to be present in ks: (defn order-fn ([ks] (order-fn ks #(.indexOf %1 %2))) ([ks ord-fn] (letfn [(-ord-fn [e] (let [o (ord-fn ks e)] (if (> o -1) o (throw (RuntimeException. (format "'%s' not found in %s" e ks))))))] #(compare (-ord-fn %1) (-ord-fn %2))))) Is there something like this in clojure.core already or a more elegant implementation? -- 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/d/optout.