【发布时间】:2023-03-04 21:55:01
【问题描述】:
我希望压缩我的应用程序的网络流量。
根据(最新?)"Haskell Popularity Rankings",zlib 似乎是一个非常流行的解决方案。 zlib的接口使用ByteStrings:
compress :: ByteString -> ByteString
decompress :: ByteString -> ByteString
我使用的是常规的Strings,这也是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有几种类型?有哪些类型,为什么? - 将
Strings 转换为ByteStrings 的“方法”是什么?
顺便说一句,我发现它确实适用于 Data.ByteString.Lazy.Char8 的 ByteString,但我仍然很感兴趣。
【问题讨论】:
标签: haskell bytestring