Proposal: add 'equating' function to Data.List
Greg Weber
greg at gregweber.info
Fri Jul 18 19:57:23 UTC 2014
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/20140718/7be751cc/attachment.html>
More information about the Libraries
mailing list