Proposal: add 'equating' function to Data.List

Frerich Raabe raabe at
Fri Jul 18 19:26:16 UTC 2014


A common use case for 'on' (from Data.Function) is to use it with 'compare', 
e.g. 'compare `on` snd'. In fact, this pattern is so common that there's a 
convenient 'comparing' function which provides a shortcut for this use case 
such that one can write

   sortBy (comparing snd)

instead of

   sortBy (compare `on` snd)

I think another common use case is to use 'on' together with (==) as in

   groupBy ((==) `on` snd)

In a similiar vein as with 'comparing', I think it would be nice if there 
was a function which encapsulates this use case, like

   equating :: Eq b => (a -> b) -> a -> a -> Bool
   equating = on (==)

such that one can write

   groupBy (equating snd)

In fact, groupBy is just one of many *By functions taking an a -> a -> Bool 
-- many of which are Data.List, e.g. groupBy, nubBy, deleteBy, intersectBy, 
unionBy. Hence, it seems plausible to define 'equating' in Data.List. This is 
the same reasoning as why 'comparing' is in Data.Ord: because the module 
exposes a lot of *By functions taking an a -> a -> Ordering.

- Frerich

More information about the Libraries mailing list