On Fri, Sep 3, 2010 at 5:34 PM, Ian Lynagh <span dir="ltr">&lt;<a href="mailto:igloo@earth.li">igloo@earth.li</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<div class="im">On Fri, Sep 03, 2010 at 04:14:41PM +0200, Johan Tibell wrote:<br>
&gt; On Fri, Sep 3, 2010 at 8:46 AM, Brian Bloniarz &lt;<a href="mailto:brian.bloniarz@gmail.com">brian.bloniarz@gmail.com</a>&gt;wrote:<br>
&gt;<br>
&gt; &gt; On Sun, 29 Aug 2010 15:34:29 +0200<br>
&gt; &gt; Johan Tibell &lt;<a href="mailto:johan.tibell@gmail.com">johan.tibell@gmail.com</a>&gt; wrote:<br>
&gt; &gt; &gt; Proposal: Add strict versions of foldlWithKey and insertLookupWithKey to<br>
&gt; &gt; &gt; Data.Map<br>
&gt; &gt;<br>
&gt; &gt; +1, insertLookupWithKey&#39; I could have used recently.<br>
&gt; &gt;<br>
&gt; &gt; Is there some reason why all the other WithKey functions don&#39;t require<br>
&gt; &gt; strict variants? Asked another way, suppose I want to build a Map via<br>
&gt; &gt; foldl&#39; (unionWith (+)) without space leaks, is that possible?<br>
&gt; &gt;<br>
&gt;<br>
&gt; No reason, expect that I didn&#39;t have a need for it. Every function that<br>
&gt; takes a higher-order argument that combines two values needs a strict<br>
&gt; variant.<br>
<br>
</div>Adding foldlWithKey&#39; but not foldrWithKey&#39; seems particularly odd to me.<br></blockquote><div><br>Will add foldrWithKey&#39; as as well (these are terrible names btw).<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">


<div class="im">&gt; -- | /O(n)/. A strict version of &#39;foldlWithKey&#39;.<br>
&gt; foldlWithKey&#39; :: (b -&gt; k -&gt; a -&gt; b) -&gt; b -&gt; Map k a -&gt; b<br>
&gt; foldlWithKey&#39; f = go<br>
&gt;   where<br>
&gt;     go z Tip              = z<br>
&gt;     go z (Bin _ kx x l r) = z `seq` go (f (go z l) kx x) r<br>
<br>
</div>As discussed in the thread for your previous proposal, I think the<br>
(go z l) should be forced too:<br>
    <a href="http://www.haskell.org/pipermail/libraries/2010-August/014091.html" target="_blank">http://www.haskell.org/pipermail/libraries/2010-August/014091.html</a><br></blockquote><div><br>I&#39;ll fix that.<br> </div>

<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">&gt; -- | /O(log n)/. A strict version of &#39;insertLookupWithKey&#39;.<br>
&gt; insertLookupWithKey&#39; :: Ord k =&gt; (k -&gt; a -&gt; a -&gt; a) -&gt; k -&gt; a -&gt; Map k a<br>
&gt;                      -&gt; (Maybe a, Map k a)<br>
&gt; insertLookupWithKey&#39; f kx x = kx `seq` go<br>
&gt;   where<br>
&gt;     go Tip = x `seq` (Nothing, singleton kx x)<br>
&gt;     go (Bin sy ky y l r) =<br>
&gt;         case compare kx ky of<br>
&gt;             LT -&gt; let (found, l&#39;) = go l<br>
&gt;                   in (found, balance ky y l&#39; r)<br>
&gt;             GT -&gt; let (found, r&#39;) = go r<br>
&gt;                   in (found, balance ky y l r&#39;)<br>
&gt;             EQ -&gt; let x&#39; = f kx x y in x&#39; `seq` (Just y, Bin sy kx x&#39; l r)<br>
<br>
</div>I&#39;m not sure whether it makes more sense to always force x here. I guess<br>
it probably doesn&#39;t.<br></blockquote><div><br>I&#39;ll look at whatever insertWithKey&#39; does and do the same for consistency.<br><br>Thanks for the feedback.<br><br>-- Johan<br><br></div></div>