[Haskell-beginners] Printing the entries of a Data.Map m

Felipe Almeida Lessa felipe.lessa at gmail.com
Sun May 15 18:49:26 CEST 2011


On Sun, May 15, 2011 at 11:39 AM, Manfred Lotz <manfred.lotz at arcor.de> wrote:
> I also tried something similar, and indeed you are right. mapM_ in
> conjunction with toList is better in terms of memory and runtime
> (mainly because GC is less busy) than using functions from Traversable.

If you want just the side effects you shouldn't be using Traversable,
but Foldable.  In particular [1],

  mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()

Doing the same test as Daniel Fischer's, but with an additional definition

  import qualified Data.Foldable as F

  perFoldable :: (Ord k, Show v) => Map k v -> IO ()
  perFoldable = F.mapM_ print

which is also the shortest definition, I get the following results:

  viaList:     208 MiB total memory, 2.24s MUT time, 1.11s GC time,
3.35s total time
  viaElems:    208 MiB total memory, 1.40s MUT time, 1.13s GC time,
2.53s total time
  perTraverse: 322 MiB total memory, 1.77s MUT time, 2.84s GC time,
4.61s total time
  perFoldable: 215 MiB total memory, 1.53s MUT time, 1.73s GC time,
3.26s total time

Cheers,

[1] http://hackage.haskell.org/packages/archive/base/4.3.1.0/doc/html/Data-Foldable.html#v:mapM_

-- 
Felipe.



More information about the Beginners mailing list