[Haskell-cafe] Strict version of Data.Map.map
Edsko de Vries
devriese at cs.tcd.ie
Fri Feb 27 09:39:52 EST 2009
> I guess so. Maybe using mapAccum helps:
> import qualified Data.Map as M
> strictMap :: (a -> b) -> M.Map k a -> M.Map k b
> strictMap f m = case M.mapAccum f' () m of
> ((), m') -> m'
> where f' () x = x' `seq` ((), x') where x' = f x
> testStrictness mapper = m `seq` "Not strict."
> where m = mapper (const e) (M.singleton () ())
> e = error "Strict!" :: ()
Very clever. I had tried to use mapAccum but I couldn't figure out
what to put in the accumulator. I didn't realize it didn't make a
difference (unit will do) as long as it is evaluated when the Map is.
Seq wrecks my head ;)
More information about the Haskell-Cafe