mapM_ for bytestring

Nicolas Trangez nicolas at incubaid.com
Wed Sep 11 15:56:25 CEST 2013


On Wed, 2013-09-11 at 14:35 +0100, Duncan Coutts wrote:
> For mapM etc, personally I think a better solution would be if
> ByteString and Text and other specialised containers could be an
> instance of Foldable/Traversable.

Yes!

>  Those classes define mapM etc but
> currently they only work for containers that are polymorphic in their
> elements, so all specialised containers are excluded.

Indeed :-( Hence my question a couple of days ago.

>  I'm sure there
> must be a solution to that (I'd guess with type families) and that
> would
> be much nicer than adding mapM etc to bytestring itself. We would then
> just provide efficient instances for Foldable/Traversable.

I looked into this when this thread started, but bumped into at least
one 'issue': when defining (OTOH) something like

import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

class Foldable t where
    type Elem c :: *
    foldr :: (Elem t -> b -> b) -> b -> t -> b

instance Foldable BS.ByteString where
    type Elem BS.ByteString = Word8
    foldr = BS.foldr

instance Foldable BS8.ByteString where
    type Elem BS8.ByteString = Char
    foldr = BS8.foldr

which fails because BS.ByteString and BS8.ByteString are the same.

Nicolas




More information about the Libraries mailing list