Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Signature of monadic functions (Chadda? Fouch?)


----------------------------------------------------------------------

Message: 1
Date: Tue, 22 Oct 2013 13:41:24 +0200
From: Chadda? Fouch? <chaddai.fou...@gmail.com>
To: beginners <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Signature of monadic functions
Message-ID:
        <canfjzrajvv05ae2cpgy7tay-qrwg-drs98e-bo7rqm5_nbm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Le 17 oct. 2013 12:57, "Daniel Trstenjak" <daniel.trsten...@gmail.com> a
?crit :
>
>
> Hi Lorenzo,
>
> all the nice abstractions like Functor, Traversable or Foldable operate
> on the values of the Map. So there's 'Data.Traversable.mapM', which
> almost does what you want, but only for the values of the Map.
>
> Ok, here's a solution that does what you want:
>
> import Data.Map
> import Control.Monad
>
> mapKeysM :: (Ord k1, Ord k2, Monad m) => (k1 -> m k2) -> Map k1 v -> m
(Map k2 v)
> mapKeysM f map = return . fromList =<< mapM g (toList map)
>   where
>    g (key, value) = do
>       key' <- f key
>       return (key', value)
>

If you're worried that constructing the whole list then consuming it is too
much (use of mapM precludes a good streaming), you could use foldM to
construct the Map immediately, which would probably be exactly equivalent
to what mapKeys has to be doing (changing the keys obviously means
rebuilding the Map from scratch).

-- 
Chadda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131022/06b117ee/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 64, Issue 35
*****************************************

Reply via email to