Add Data.Eq.equating to match Data.Ord.comparing?

I recently wanted to group a list of pairs by the second item in the pairs. So I tried writing
x = groupBy (comparing snd) pairs
But this threw a type error because groupBy expects its first argument to return a Bool and comparing returns an Ordering. So I had to write
x = groupBy ((== EQ) . comparing snd) pairs
Which is clunky. What I needed was a function in Data.Eq such as
equating :: Eq a => (b -> a) -> b -> b -> Bool
This could be generalised to a function such as
pairWise :: (b -> b -> c) -> (a -> b) -> a -> a -> c pairWise pair project x y = pair (project x) (project y)
Then we can write
comparing = pairWise compare equating = pairWise (==)
Should I submit a patch to add this? And is "pairWise" the right name? Paul.

Hi
This could be generalised to a function such as
pairWise :: (b -> b -> c) -> (a -> b) -> a -> a -> c pairWise pair project x y = pair (project x) (project y)
We call this function "on", and I believe it has been recently added to the standard libraries.
comparing = pairWise compare equating = pairWise (==)
"comparing" has certainly been added recently, and I think "equating" as well. http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Ord.html#v%3Acom... Thanks Neil

pairWise :: (b -> b -> c) -> (a -> b) -> a -> a -> c pairWise pair project x y = pair (project x) (project y)
We call this function "on", and I believe it has been recently added to the standard libraries.
x = groupBy (equating snd) pairs
In Data.Functions. thus becomes x = groupBy ((==) `on` snd) pairs Regards, Malcolm

On Thu, 20 Sep 2007, "Neil Mitchell"
"comparing" has certainly been added recently, and I think "equating" as well.
No, equating has not been added, since no consensus was reached on its name: http://www.haskell.org/pipermail/libraries/2006-November/006219.html http://www.haskell.org/pipermail/libraries/2006-November/006223.html -- /NAD

On Thu, Sep 20, 2007 at 11:26:48AM +0100, Paul Johnson wrote:
I recently wanted to group a list of pairs by the second item in the pairs. So I tried writing
x = groupBy (comparing snd) pairs
But this threw a type error because groupBy expects its first argument to return a Bool and comparing returns an Ordering. So I had to write
x = groupBy ((== EQ) . comparing snd) pairs
Which is clunky. What I needed was a function in Data.Eq such as
equating :: Eq a => (b -> a) -> b -> b -> Bool
This could be generalised to a function such as
pairWise :: (b -> b -> c) -> (a -> b) -> a -> a -> c pairWise pair project x y = pair (project x) (project y)
Then we can write
comparing = pairWise compare equating = pairWise (==)
Should I submit a patch to add this? And is "pairWise" the right name?
No, because it's already in the standard library (Data.Function.on, intended use (==) `on` snd) Stefan
participants (5)
-
Malcolm Wallace
-
Neil Mitchell
-
Nils Anders Danielsson
-
Paul Johnson
-
Stefan O'Rear