Add Data.Eq.equating to match Data.Ord.comparing?
Stefan O'Rear
stefanor at cox.net
Thu Sep 20 10:10:34 EDT 2007
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/libraries/attachments/20070920/f39aaf4e/attachment.bin
More information about the Libraries
mailing list