我希望压缩应用程序的网络流量。
根据(最新的?) "Haskell Popularity Rankings",zlib似乎是一个相当流行的解决方案。zlib的接口使用ByteString
s:
compress :: ByteString -> ByteString
decompress :: ByteString -> ByteString
我使用的是常规String
,它们也是read
、show
和Network.Socket
使用的数据类型
sendTo :: Socket -> String -> SockAddr -> IO Int
recvFrom :: Socket -> Int -> IO (String, Int, SockAddr)
因此,为了压缩字符串,我需要某种方法将String
转换为ByteString
,反之亦然。在hoogle的帮助下,我发现:
Data.ByteString.Char8 pack :: String -> ByteString
尝试使用它:
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")
失败,因为(?)有不同类型的ByteString
?
所以基本上:
ByteString
有几种类型吗?什么类型?为什么?String
s转换为why的方法?顺便说一句,我发现它可以在Data.ByteString.Lazy.Char8
的ByteString
上运行,但我仍然很感兴趣。
https://stackoverflow.com/questions/1451755
复制相似问题