On Fri, Sep 3, 2010 at 5:34 PM, Ian Lynagh <span dir="ltr"><<a href="mailto:igloo@earth.li">igloo@earth.li</a>></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>
> On Fri, Sep 3, 2010 at 8:46 AM, Brian Bloniarz <<a href="mailto:brian.bloniarz@gmail.com">brian.bloniarz@gmail.com</a>>wrote:<br>
><br>
> > On Sun, 29 Aug 2010 15:34:29 +0200<br>
> > Johan Tibell <<a href="mailto:johan.tibell@gmail.com">johan.tibell@gmail.com</a>> wrote:<br>
> > > Proposal: Add strict versions of foldlWithKey and insertLookupWithKey to<br>
> > > Data.Map<br>
> ><br>
> > +1, insertLookupWithKey' I could have used recently.<br>
> ><br>
> > Is there some reason why all the other WithKey functions don't require<br>
> > strict variants? Asked another way, suppose I want to build a Map via<br>
> > foldl' (unionWith (+)) without space leaks, is that possible?<br>
> ><br>
><br>
> No reason, expect that I didn't have a need for it. Every function that<br>
> takes a higher-order argument that combines two values needs a strict<br>
> variant.<br>
<br>
</div>Adding foldlWithKey' but not foldrWithKey' seems particularly odd to me.<br></blockquote><div><br>Will add foldrWithKey' 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">> -- | /O(n)/. A strict version of 'foldlWithKey'.<br>
> foldlWithKey' :: (b -> k -> a -> b) -> b -> Map k a -> b<br>
> foldlWithKey' f = go<br>
> where<br>
> go z Tip = z<br>
> 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'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">> -- | /O(log n)/. A strict version of 'insertLookupWithKey'.<br>
> insertLookupWithKey' :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a<br>
> -> (Maybe a, Map k a)<br>
> insertLookupWithKey' f kx x = kx `seq` go<br>
> where<br>
> go Tip = x `seq` (Nothing, singleton kx x)<br>
> go (Bin sy ky y l r) =<br>
> case compare kx ky of<br>
> LT -> let (found, l') = go l<br>
> in (found, balance ky y l' r)<br>
> GT -> let (found, r') = go r<br>
> in (found, balance ky y l r')<br>
> EQ -> let x' = f kx x y in x' `seq` (Just y, Bin sy kx x' l r)<br>
<br>
</div>I'm not sure whether it makes more sense to always force x here. I guess<br>
it probably doesn't.<br></blockquote><div><br>I'll look at whatever insertWithKey' does and do the same for consistency.<br><br>Thanks for the feedback.<br><br>-- Johan<br><br></div></div>