Chouser asked me Saturday night if it would be possible to reduce the number of functions in the module. I looked at the problem, and if we want to keep the two sets of functions (returning one element and returning many elements), idiomatically, it seems like all the functions will have to stay:
* I cannot add an optional function parameter and keep varargs. It's either 2 variadic functions or 1 function that takes a collection. It seems to me that the variadic functions are preferable as they are more similar to max/min. * I could force the user the pass a comparison function, but since I imagine the most common case is testing identity, it would quickly become an annoyance to write (greatest identity x y) all the time. * I could remove the functions that return only one result and instead have users manually call first on the results. Because I think it is a lot more common to want to find only one greatest/least element, this would also quickly become an annoyance to users. Any comments or thoughts? Vincent. On Jan 24, 6:57 pm, Vincent Foley <[email protected]> wrote: > I've worked on the library today, and imported it into a GitHub > project:http://github.com/gnuvince/clojure-greatest-least/tree/master > > I've added a variation of all four functions that returns all the > greatest/least elements as well as a small test suite. > > Could this be an interesting addition to clojure-contrib? > > Vincent. > > On Jan 21, 11:09 pm, Vincent Foley <[email protected]> wrote: > > > 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 [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 -~----------~----~----~----~------~----~------~--~---
