Gmane
Picon
From: Paul Johnson <paul <at> cogito.org.uk>
Subject: Add Data.Eq.equating to match Data.Ord.comparing?
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2007-09-20 10:26:48 GMT (34 weeks, 17 hours and 3 minutes ago)
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.