I want to write a simple function which splits a ByteString
into [ByteString]
using '\n'
as the delimiter. My attempt:
import Data.ByteString
listize :: ByteString -> [ByteString]
listize xs = Data.ByteString.splitWith (=='\n') xs
This throws an error because '\n'
is a Char
rather than a Word8
, which is what Data.ByteString.splitWith
is expecting.
How do I turn this simple character into a Word8
that ByteString
will play with?
You could just use the numeric literal
10
, but if you want to convert the character literal you can usefromIntegral (ord '\n')
(thefromIntegral
is required to convert theInt
thatord
returns into aWord8
). You'll have to importData.Char
forord
.You could also import
Data.ByteString.Char8
, which offers functions for usingChar
instead ofWord8
on the sameByteString
data type. (Indeed, it has alines
function that does exactly what you want.) However, this is generally not recommended, asByteString
s don't store Unicode codepoints (which is whatChar
represents) but instead raw octets (i.e.Word8
s).If you're processing textual data, you should consider using
Text
instead ofByteString
.