Comparing on multiple criteria

Hello all, Data.Ord has a handy function called comparing, and its documentation shows an example of its use. But what if you want to sort a list of values based on multiple criteria? It turns out there is a neat way to do this: compareTuple = mconcat [comparing fst, comparing snd] The default Monoid instances for Ordering and functions work exactly as required here. (Thanks to vixey in #haskell for the hint to look at monoids!) To reverse the order of a criterion or a set of criteria, flip can be used: compareTuple' = mconcat [comparing fst, flip $ comparing snd] I think it would be really neat if these two use cases were also described in comparing's documentation. Who is the right person to ask this of? Thanks in advance, Martijn.

On Dec 21, 2008, at 8:52 AM, Martijn van Steenbergen wrote:
Hello all,
Data.Ord has a handy function called comparing, and its documentation shows an example of its use.
But what if you want to sort a list of values based on multiple criteria? It turns out there is a neat way to do this:
compareTuple = mconcat [comparing fst, comparing snd]
The default Monoid instances for Ordering and functions work exactly as required here. (Thanks to vixey in #haskell for the hint to look at monoids!)
Indeed, this is great to know. I can't help but notice that there is no documentation of any kind at all for the Monoid instance of Ordering; how were we supposed to know this behavior existed in the first place, except by hunting down the source code for the instance declaration? -Jan-Willem Maessen

On Sun, Dec 21, 2008 at 9:20 AM, Jan-Willem Maessen
Indeed, this is great to know. I can't help but notice that there is no documentation of any kind at all for the Monoid instance of Ordering; how were we supposed to know this behavior existed in the first place, except by hunting down the source code for the instance declaration?
It seems we have no good way to associate documentation with instances. Luke

On Sun, Dec 21, 2008 at 11:20 AM, Jan-Willem Maessen
On Dec 21, 2008, at 8:52 AM, Martijn van Steenbergen wrote:
Hello all,
Data.Ord has a handy function called comparing, and its documentation shows an example of its use.
But what if you want to sort a list of values based on multiple criteria? It turns out there is a neat way to do this:
compareTuple = mconcat [comparing fst, comparing snd]
The default Monoid instances for Ordering and functions work exactly as required here. (Thanks to vixey in #haskell for the hint to look at monoids!)
Indeed, this is great to know. I can't help but notice that there is no documentation of any kind at all for the Monoid instance of Ordering; how were we supposed to know this behavior existed in the first place, except by hunting down the source code for the instance declaration?
This is a great example of why it's a bad idea to introduce new
functionality with a Monoid instance. Even if you know the instance
exists, mappend is so general that it's difficult or impossible to
predict what it will do at a given type.
There should be an explicit function for combining Ordering values
lexicographically, with a note in the documentation saying that it's
the basis of the Monoid instance.
--
Dave Menendez

David Menendez schrieb:
On Sun, Dec 21, 2008 at 11:20 AM, Jan-Willem Maessen
wrote: On Dec 21, 2008, at 8:52 AM, Martijn van Steenbergen wrote:
Hello all,
Data.Ord has a handy function called comparing, and its documentation shows an example of its use.
But what if you want to sort a list of values based on multiple criteria? It turns out there is a neat way to do this:
compareTuple = mconcat [comparing fst, comparing snd]
The default Monoid instances for Ordering and functions work exactly as required here. (Thanks to vixey in #haskell for the hint to look at monoids!)
This is a great example of why it's a bad idea to introduce new functionality with a Monoid instance. Even if you know the instance exists, mappend is so general that it's difficult or impossible to predict what it will do at a given type.
There should be an explicit function for combining Ordering values lexicographically, with a note in the documentation saying that it's the basis of the Monoid instance.

David Menendez wrote:
This is a great example of why it's a bad idea to introduce new functionality with a Monoid instance. Even if you know the instance exists, mappend is so general that it's difficult or impossible to predict what it will do at a given type.
There should be an explicit function for combining Ordering values lexicographically, with a note in the documentation saying that it's the basis of the Monoid instance.
Agreed. Supposing such an explicit function exists (with proper documentation), would the original example be worth mentioning in or around comparing's documentation? If it saved the next user a couple of minutes of coming up with something similar, I'd be happy. Martijn.
participants (5)
-
David Menendez
-
Henning Thielemann
-
Jan-Willem Maessen
-
Luke Palmer
-
Martijn van Steenbergen