haskellconcatenationbytestring

Is there an append operator for ByteString?


For String there is ++, which has type

> :t (++)
(++) :: [a] -> [a] -> [a]

Evidently it doesn't work on ByteString because it isn't a list. I see the append function but is there an operator for it?


Solution

  • ByteString has a Semigroup instance, so it can be combined in the usual way that semigroups are combined, with (<>).

    The same operator works for strings as well, because String ~ [Char], and [a] has a Semigroup instance where (<>) = (++).

    Prelude Data.ByteString.Char8> unpack $ pack "abc" <> pack "def"
    "abcdef"
    

    Here I convert two Strings to ByteStrings, combine them as ByteStrings, and then convert back to String to demonstrate that it's worked.