<div dir="ltr">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.<div>What I would like to see is a function groupOn next to groupBy.</div>

<div><br></div><div>groupOn :: Eq b => (a -> b) -> [a] -> [[a]]</div><div><br></div><div>Then equating is no longer needed, and one just writes: groupOn snd</div><div>I believe this style also gives better opportunity for optimization (Scwartzian transform).</div>

<div><br></div><div>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.</div><div>All of this is solved in mono-traversable right now by the groupAllOn function [1]</div>

<div><br></div><div>[1] <a href="http://hackage.haskell.org/package/mono-traversable-0.6.0.4/docs/Data-Sequences.html">http://hackage.haskell.org/package/mono-traversable-0.6.0.4/docs/Data-Sequences.html</a></div></div><div class="gmail_extra">

<br><br><div class="gmail_quote">On Fri, Jul 18, 2014 at 12:26 PM, Frerich Raabe <span dir="ltr"><<a href="mailto:raabe@froglogic.com" target="_blank">raabe@froglogic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi,<br>
<br>
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<br>


<br>
  sortBy (comparing snd)<br>
<br>
instead of<br>
<br>
  sortBy (compare `on` snd)<br>
<br>
I think another common use case is to use 'on' together with (==) as in<br>
<br>
  groupBy ((==) `on` snd)<br>
<br>
In a similiar vein as with 'comparing', I think it would be nice if there was a function which encapsulates this use case, like<br>
<br>
  equating :: Eq b => (a -> b) -> a -> a -> Bool<br>
  equating = on (==)<br>
<br>
such that one can write<br>
<br>
  groupBy (equating snd)<br>
<br>
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.<br>


<br>
- Frerich<br>
<br>
______________________________<u></u>_________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/libraries</a><br>
</blockquote></div><br></div>