A couple months ago, there was a discussion in this group about the functions max and min and making them work with data types other than numbers. I was toying around tonight, and I wrote the following functions.
(defn- boundary [compare-fn f & args] (reduce (fn [a b] (if (compare-fn (compare (f b) (f a))) b a)) args)) (defn greatest-by "Return the argument for which f returns the greatest value." [f & args] (apply boundary pos? f args)) (defn greatest "Return the greatest argument." [& args] (apply greatest-by identity args)) (defn least-by "Return the argument for which f returns the smallest value." [f & args] (apply boundary neg? f args)) (defn least "Return the smallest element." [& args] (apply least-by identity args)) user> (greatest 1 3 2) 3 user> (greatest "foo" "bar" "baz") "foo" user> (greatest-by count "foo" "bar" "abccb") "abccb" user> (least 1 3 2) 1 user> (least "foo" "bar" "baz") "bar" user> (least-by count "foo" "bar" "abccb") "foo" user> --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---