
20 Sep
2007
20 Sep
'07
6:26 a.m.
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.