Proposal: Add strict versions of foldlWithKey and
insertLookupWithKey to Data.Map
igloo at earth.li
Fri Sep 3 11:34:57 EDT 2010
On Fri, Sep 03, 2010 at 04:14:41PM +0200, Johan Tibell wrote:
> On Fri, Sep 3, 2010 at 8:46 AM, Brian Bloniarz <brian.bloniarz at gmail.com>wrote:
> > On Sun, 29 Aug 2010 15:34:29 +0200
> > Johan Tibell <johan.tibell at gmail.com> wrote:
> > > Proposal: Add strict versions of foldlWithKey and insertLookupWithKey to
> > > Data.Map
> > +1, insertLookupWithKey' I could have used recently.
> > Is there some reason why all the other WithKey functions don't require
> > strict variants? Asked another way, suppose I want to build a Map via
> > foldl' (unionWith (+)) without space leaks, is that possible?
> No reason, expect that I didn't have a need for it. Every function that
> takes a higher-order argument that combines two values needs a strict
Adding foldlWithKey' but not foldrWithKey' seems particularly odd to me.
> -- | /O(n)/. A strict version of 'foldlWithKey'.
> foldlWithKey' :: (b -> k -> a -> b) -> b -> Map k a -> b
> foldlWithKey' f = go
> go z Tip = z
> go z (Bin _ kx x l r) = z `seq` go (f (go z l) kx x) r
As discussed in the thread for your previous proposal, I think the
(go z l) should be forced too:
> -- | /O(log n)/. A strict version of 'insertLookupWithKey'.
> insertLookupWithKey' :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a
> -> (Maybe a, Map k a)
> insertLookupWithKey' f kx x = kx `seq` go
> go Tip = x `seq` (Nothing, singleton kx x)
> go (Bin sy ky y l r) =
> case compare kx ky of
> LT -> let (found, l') = go l
> in (found, balance ky y l' r)
> GT -> let (found, r') = go r
> in (found, balance ky y l r')
> EQ -> let x' = f kx x y in x' `seq` (Just y, Bin sy kx x' l r)
I'm not sure whether it makes more sense to always force x here. I guess
it probably doesn't.
More information about the Libraries