Proposal: add 'equating' function to Data.List

Greg Weber greg at gregweber.info
Mon Jul 21 00:27:02 UTC 2014


I am -1 on things that encourage converting Eq to Bool rather than just
using Eq directly.

Is there a use case for group that is not satisfied by groupOn with a
newtype with an Eq instance?
Granted, a newtype may be heavy-weight, but I feel that the current group
should be an escape hatch in the rare case that groupOn does not suffice,
not something we codify via Fairbairn threshold because that is the only
API that exists today. I can create a separate proposal for adding groupOn,
etc.


On Fri, Jul 18, 2014 at 12:57 PM, Greg Weber <greg at gregweber.info> wrote:

> I think the `By` functions that expect a Bool are all cumbersome because
> they are too flexible. 100% of the time I personally use these functions I
> want to use Ord or Eq.
> What I would like to see is a function groupOn next to groupBy.
>
> groupOn :: Eq b => (a -> b) -> [a] -> [[a]]
>
> Then equating is no longer needed, and one just writes: groupOn snd
> I believe this style also gives better opportunity for optimization
> (Scwartzian transform).
>
> Of course, this function is still problematic because it operates only on
> lists and does not group over the entire list, but those are separate
> issues.
> All of this is solved in mono-traversable right now by the groupAllOn
> function [1]
>
> [1]
> http://hackage.haskell.org/package/mono-traversable-0.6.0.4/docs/Data-Sequences.html
>
>
> On Fri, Jul 18, 2014 at 12:26 PM, Frerich Raabe <raabe at froglogic.com>
> wrote:
>
>> Hi,
>>
>> 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
>>
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> http://www.haskell.org/mailman/listinfo/libraries
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20140720/b6ed4add/attachment.html>


More information about the Libraries mailing list