Proposal: Add `maxOn` and `minOn` functions to Data.Ord

Edward Kmett ekmett at gmail.com
Fri May 27 19:02:43 UTC 2016


Note: in general we have chosen to supply By methods, rather than On methods, because he on combinator can be used to get one in terms of the other. We explicitly added sortOn because it can use a Schwartzian transform to be more efficient than sortBy (comparing `on` f).

These don't have direct analogues there, however. So that usual argument against adding these sorts of methods doesn't hold up.

One thing of note is that what you are asking for here is usually called argmin and argmax. Perhaps consistency with the rest of mathematics should trump consistency with sortOn.

IIRC, We already have Arg in Data.Semigroup for use with the Min and Max semigroups for this purpose, so such naming would fit.

-Edward

> On May 27, 2016, at 2:51 PM, Daniel Díaz Casanueva <dhelta.diaz at gmail.com> wrote:
> 
> I think these are useful in general, and that they would make a good addition to `Data.Ord`. The names are inspired by `sortOn`. Definitions:
> 
> ```
> minOn :: Ord b => (a -> b) -> a -> a -> a
> minOn f x y =
>   case comparing f x y of
>     LT -> x
>     _ -> y
> 
> maxOn :: Ord b => (a -> b) -> a -> a -> a
> maxOn f x y =
>   case comparing f x y of
>     GT -> x
>     _ -> y
> ```
> 
> These don't need to be the exact definitions, but they convey the semantics I'm looking for.
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


More information about the Libraries mailing list