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

Paul Johnson paul at cogito.org.uk
Thu Sep 20 06:26:48 EDT 2007


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.



More information about the Libraries mailing list