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 ;)

Thanks!

Edsko
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to