[Haskell-beginners] Printing the entries of a Data.Map m
Manfred Lotz
manfred.lotz at arcor.de
Sun May 15 10:51:19 CEST 2011
Hi Matthias,
On Sun, 15 May 2011 10:15:42 +0200
Matthias Guedemann <matthias.guedemann at ovgu.de> wrote:
>
> Hi Manfred,
>
> > I may be wrong but having to use Map.toList looks pretty
> > inefficient.
> >
> > Question: I'd like to know if there is a more efficient way to do
> > it?
>
> I do not know if it is really more efficient (it has to consider each
> entry, just like converting to list does), but Data.Map is an instance
> of Data.Traversable, which has the traverse function, see:
>
> http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.3.1.0/Data-Traversable.html#t:Traversable
>
> e.g.
>
> Prelude> :m Data.Map Data.Traversable
> Prelude Data.Map Data.Traversable> let mymap = insert 1 "bubu" $
> insert 2 "baba" $ empty Prelude Data.Map Data.Traversable> traverse
> print mymap "bubu"
> "baba"
> fromList [(1,()),(2,())]
> Prelude Data.Map Data.Traversable>
>
>
Thanks for pointing me to Traversable. Now I used Traversable.mapM
which worked fine and seems to be the "traversable" counterpart of mapM_
from Control.Monad.
It didn't make much of a difference in runtime doing it without toList.
I'm not quite sure if it is really more efficient or not. Perhaps I
would have to create dictonary with some millions entries in order to
see a noticable difference?
--
Manfred
More information about the Beginners
mailing list