我希望压缩我的应用程序的网络流量。
根据(最新?)"Haskell Popularity Rankings" , zlib似乎是一个非常受欢迎的解决方案。 zlib的接口(interface)使用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
转换为ByteString
的“方法”是什么?
顺便说一句,我发现它确实可以与 Data.ByteString.Lazy.Char8
的 ByteString
配合使用,但我仍然很感兴趣。
最佳答案
有两种字节串:严格(在 Data.Bytestring.Internal 中定义)和惰性(在 Data.Bytestring.Lazy.Internal 中定义)。正如您所发现的,zlib 使用惰性字节串。
关于haskell - 多种类型的字符串(ByteString),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1451755/