<p dir="ltr">Agreed. I was just playing devil's advocate.</p>
<div class="gmail_quote">On Mar 8, 2016 4:14 PM, "Edward Kmett" <<a href="mailto:ekmett@gmail.com">ekmett@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">+1 on adding the methods, but I'd really rather see it done without incurring spurious constraints that they don't need.<div><br></div><div>We just went through and cleaned up a few similar unused and unusable constraints in base on various array operations. This seems to beg us to do the same later, and we don't bother to wastefully pass in Ord constraints on any other combinators in Data.Set or Data.Map, so why start now?</div><div><br></div><div>-Edward</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 7, 2016 at 7:14 PM, Gabriel Gonzalez <span dir="ltr"><<a href="mailto:gabriel439@gmail.com" target="_blank">gabriel439@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 style="word-wrap:break-word">I would like to propose adding `take`/`drop`/`splitAt` to both `Data.Map` and `Data.Set` as originally requested in:<div><br></div><div><a href="https://github.com/haskell/containers/issues/135" target="_blank">https://github.com/haskell/containers/issues/135</a></div><div><br></div><div>The motivation behind this proposal is three-fold:</div><div><br></div><div>* for convenience - these functions are commonly used to implement pagination or previews of maps/sets</div><div>* for type accuracy - the public API impose an unnecessary `Ord` constraint</div><div>* for efficiency - these can be implemented more efficiently using the internal API</div><div><br></div><div>Currently the only way you can implement this functionality via the public API is to use `lookupIndex`/`elemAt` + `split`.  For example, one way to implement `Data.Set.take` is:<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><br></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">take :: Ord a => Int -> Set a -> Set a</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">take n m</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    | n      <  0 = empty</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    | size m <= n = m</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    | otherwise   = lt</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">  where</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    (lt, _) = split k m</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    k       = elemAt n m</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">{-# INLINE take #-}</div></div></blockquote><div><div><br></div><div>This implementation incurs an unnecessary `Ord` constraint due to a roundabout way of computing `take`: this extracts the element at the given index and then works backwards from the element’s value to partition the set using O(log N) comparisons.  We could eliminate all of the comparisons by using the internal API.</div><div><br></div><div>Intuitively, we expect that the performance of `Data.Set.take` would benefit from avoiding those unnecessary comparisons and also avoiding traversing the `Set`’s spine twice.  So I tested that hypothesis by implementing `take` via the internal API like this:</div></div></div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">take :: Int -> Set a -> Set a</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">take n0 s0 = go s0 n0</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">  where</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    go s@(Bin sz x l r) n =</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        if sz <= n</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        then s</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        else</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            let sl = size l</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            in  if n <= sl</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">                then go l n</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">                else link x l (go r $! n - sl)</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    go Tip _ = Tip</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">{-# INLINE take #-}</div></div></blockquote><div><br></div><div>I then added the following benchmark to `benchmarks/Set.hs`:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><b>diff --git a/benchmarks/Set.hs b/benchmarks/Set.hs</b></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><b>index 3a6e8aa..03c99fb 100644</b></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><b>--- a/benchmarks/Set.hs</b></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><b>+++ b/benchmarks/Set.hs</b></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,187,199)">@@ -31,6 +31,7 @@<span style="color:#000000"> main = do</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">         , bench "union" $ whnf (S.union s_even) s_odd</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">         , bench "difference" $ whnf (S.difference s) s_even</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">         , bench "intersection" $ whnf (S.intersection s) s_even</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(52,189,38)">+        , bench "take" $ whnf (S.take (2^11)) s</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">         , bench "fromList" $ whnf S.fromList elems</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">         , bench "fromList-desc" $ whnf S.fromList (reverse elems)</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">         , bench "fromAscList" $ whnf S.fromAscList elems</div></div></blockquote><div><br></div><div>Here is the performance on my machine when implementing `take` via the public API:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">benchmarking take</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">time                 272.8 ns   (266.7 ns .. 278.1 ns)</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">                     0.997 R²   (0.996 R² .. 0.998 R²)</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">mean                 266.3 ns   (261.8 ns .. 270.8 ns)</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">std dev              15.44 ns   (13.26 ns .. 18.95 ns)</div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">variance introduced by outliers: 75% (severely inflated)</div></div></blockquote><div><br></div><div><div>… and the performance improved by 61% from using the internal API:</div></div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">benchmarking take</div><div style="margin:0px;line-height:normal">time                 169.2 ns   (166.1 ns .. 172.6 ns)</div><div style="margin:0px;line-height:normal">                     0.997 R²   (0.996 R² .. 0.998 R²)</div><div style="margin:0px;line-height:normal">mean                 172.1 ns   (169.4 ns .. 175.4 ns)</div><div style="margin:0px;line-height:normal">std dev              10.68 ns   (8.420 ns .. 15.34 ns)</div><div style="margin:0px;line-height:normal">variance introduced by outliers: 78% (severely inflated)</div></div></blockquote><div><br></div><div>… and I’m guessing (but haven’t tested) that the performance gap would only increase the more expensive the comparison function gets.</div><div><br></div><div>I haven’t performed comparative performance testing for `drop`/`splitAt` nor have I tested `Map` (because the benchmarks take a while for me to build and run) but I can perform those additional comparisons upon requests if people feel they are necessary.</div><div><br></div><div>I haven’t yet written up a full patch since the maintainer asked me to first run this proposal by the libraries mailing list to assess whether it would be wise to expand the `containers` API to include these utilities.</div><div><br></div><div>The deadline for discussion is two weeks.</div></div><br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
<br></blockquote></div>