I wish to compress my application's network traffic.
According to the (latest?) "Haskell Popularity Rankings", zlib seems to be a pretty popular solution. zlib's interface uses ByteString
s:
compress :: ByteString -> ByteString
decompress :: ByteString -> ByteString
I am using regular String
s, which are also the data types used by read
, show
, and Network.Socket
:
sendTo :: Socket -> String -> SockAddr -> IO Int
recvFrom :: Socket -> Int -> IO (String, Int, SockAddr)
So to compress my strings, I need some way to convert a String
to a ByteString
and vice-versa.
With hoogle's help, I found:
Data.ByteString.Char8 pack :: String -> ByteString
Trying to use it:
Prelude Codec.Compression.Zlib Data.ByteString.Char8> compress (pack "boo")
<interactive>:1:10:
Couldn't match expected type `Data.ByteString.Lazy.Internal.ByteString'
against inferred type `ByteString'
In the first argument of `compress', namely `(pack "boo")'
In the expression: compress (pack "boo")
In the definition of `it': it = compress (pack "boo")
Fails, because (?) there are different types of ByteString
?
So basically:
- Are there several types of
ByteString
? What types, and why? - What's "the" way to convert
String
s toByteString
s?
Btw, I found that it does work with Data.ByteString.Lazy.Char8
's ByteString
, but I'm still intrigued.
Char8
modules just give a different interface to the same bytestrings. – Toowoomba