darcs patch: Added Data.Map.insertWith'

Adrian Hey ahey at iee.org
Wed Sep 6 02:51:42 EDT 2006


John Goerzen wrote:
> +-- | Strict version of 'insertWith'.
> +insertWith' :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
> +insertWith' f k v m 
> +  = case lookup k m of
> +      Nothing -> insert k v m
> +      Just w -> (insert k $! f w v) m
> + 
> }

Dunno if the Data.Map you're patching is the one using AVL trees, but if 
so I would advise against this implementation as it requires two
searches for the key. The AVL library already contains functions to
do this with only one search (just needs the appropriate wrapper
writing).

Regards
--
Adrian Hey


More information about the Libraries mailing list