mapM_ for bytestring

Henning Thielemann lemming at henning-thielemann.de
Sun Sep 1 15:16:49 CEST 2013


On Sun, 1 Sep 2013, Artyom Kazak wrote:

> On Sun, 01 Sep 2013 15:18:32 +0400, Erik de Castro Lopo 
> <mle+hs at mega-nerd.com> wrote:
>
>> Err, mapM is defined as:
>>
>>     mapM :: Monad m => (a -> m b) -> [a] -> m [b]
>
> Yeah, the signatures for mapM_ and mapM would respectively be
>
>     mapM_ :: Monad m => (Word8 -> m b)     -> ByteString -> m ()

This one should not be a big problem, but could be done by calling mapM_ 
on the result of ByteString.unpack.

>     mapM  :: Monad m => (Word8 -> m Word8) -> ByteString -> m ByteString

This one can become very inefficient. Think of m = [], then many 
ByteStrings have to be constructed by repreated ByteString.cons which 
requires a lot of copying. For lists "cons" aka (:) is efficient because 
the tails are shared.




More information about the Libraries mailing list