<div dir="ltr">Enthusiastically +1<div><br></div><div>Every package I know that provides something like Foldable (ListLike, mono-traversable, a few others) includes these, largely for performance reasons.<div><br></div><div>I'm not entirely convinced with the reasoning that sum and product may be calculated in parallel though.  IMHO for structures that care about parallel reductions, I think foldMap should be the main entry point.  But I can see these might be cached.</div><div><br></div><div>John L.</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 18, 2014 at 4:26 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>After discussing these matters some more with Edward Kmett and Reid Barton, it appears the following makes sense:<br><br></div>Add the following to Foldable:<br>length—often stored as a separate field for O(1) access<br>null—called very often and slightly faster than foldr (\_ _ -> False) True for some containers<br>toList—may be the identity or nearly so, and this fact may not be statically available to the foldr/id rule<br>sum, product—may be cached internally in tree nodes for fast update, giving O(1) access; may be calculated in parallel.<br>maximum, minimum—O(n) for a search tree; O(1) for a finger tree.<br>elem—O(log n) for search trees; likely better for hash tables and such.<br><br></div>Don't add anything to Traversable, because scanl1 and scanr1 are not worth the extra dictionary weight.<br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 18, 2014 at 6:20 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I'm sorry to send so many emails, but elem should also be moved into the Foldable class to support optimized versions for searchable containers. We should also move maximum and minimum into the Foldable class to allow optimized versions. We need to settle the semantics of these anyway—Data.List.{maximum,minimum} and Data.Foldable.{maximum,minimum} currently behave differently (left-to-right vs. right-to-left). I think we want "whatever's best", which thankfully leaves the list version with its current semantics.<span><font color="#888888"><br><br>David</font></span><div><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 18, 2014 at 5:49 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">R.W. Barton was having trouble spitting out the words "length of a set", and the same holds for all sorts of other things, but unfortunately I think you're right. Proposal amended: put length in the Foldable class.<br></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 18, 2014 at 5:43 PM, Edward Kmett <span dir="ltr"><<a href="mailto:ekmett@gmail.com" target="_blank">ekmett@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Choosing the name `size` isn't free.<div><br></div><div>If we chose `length` then the existing code continues to work, code that was hiding it on import continues to hide it and you don't get conflicts.</div><div><br></div><div>Picking 'size' is compatible with common practice, but means a ton of modules would break. </div><div><br></div><div>Given the two solutions, one that doesn't break anything and one that does, with negligible differences between them otherwise, I'd prefer the one that causes the least amount of pain.</div><div><br></div><div>-Edward</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Thu, Sep 18, 2014 at 5:37 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><div><div><div>To go along with Herbert Valerio Riedel's moving functions from Foldable and Traversable to Prelude, I realized that `null` and `size` make sense for all Foldables; Reid W. Barton noted that these can have optimized implementations for many containers. He also noted that scanl1 and scanr1 make sense for general Traversables. I therefore propose:<br><br></div>Add `null` and `size` to the Foldable class. Default implementations:<br><br></div><div>null = foldr (\_ _ -> False) True<br><br></div><div>size = foldl' (\acc _ -> acc+1) 0<br></div><div><br></div>Add `scanl1` and `scanr1` to the Traversable class. Default implementations are a little less simple.<span><font color="#888888"><br><br>David<br></font></span></div></div>
<br></div></div>_______________________________________________<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/mailman/listinfo/libraries</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div></div></div></div>
</blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
<br></blockquote></div><br></div>