许多类型的字符串(ByteString) [英] Many types of String (ByteString)

查看:157
本文介绍了许多类型的字符串(ByteString)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望压缩我的应用程序的网络流量。根据(最新的?) b
$ b

-2009 /rel =noreferrer>Haskell人气排行榜 zlib 似乎是一个非常受欢迎的解决方案。 zlib的接口使用 ByteString s:

  compress :: ByteString  - > ByteString 
解压缩:: ByteString - > ByteString

我正在使用常规字符串它们也是 read show Network.Socket 使用的数据类型。 code>:

  sendTo :: Socket  - >字符串 - > SockAddr  - > IO Int 
recvFrom :: Socket - > Int - > IO(String,Int,SockAddr)

所以要压缩我的字符串,我需要一些方法来转换字符串转换为 ByteString ,反之亦然。
借助 hoogle 的帮助,我发现:

  Data.ByteString.Char8 pack :: String  - > ByteString 

尝试使用它:

  Prelude Codec.Compression.Zlib Data.ByteString.Char8>压缩(packboo)

< interactive>:1:10:
无法匹配预期的类型'Data.ByteString.Lazy.Internal.ByteString'
针对推断类型`ByteString'
在`compress'的第一个参数中,即`(packboo)'
在表达式中:compress(packboo)
在`it':it = compress(packboo)

失败,因为(?)不同类型的 ByteString



所以基本上:


  • 有几种类型的 ByteString ?什么类型,为什么?

  • 字符串 s转换为 ByteString c $ c> s?



顺便说一句,我发现它可以和 Data.ByteString.Lazy一起工作。 Char8 ByteString ,但我仍然很感兴趣。

解决方案

有两种字节串:strict(在 Data.Bytestring.Internal )和lazy(在 Data.Bytestring.Lazy.Internal )。正如您发现的那样,zlib使用懒惰的字节串。


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 ByteStrings:

compress :: ByteString -> ByteString
decompress :: ByteString -> ByteString

I am using regular Strings, 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 Strings to ByteStrings?

Btw, I found that it does work with Data.ByteString.Lazy.Char8's ByteString, but I'm still intrigued.

解决方案

There are two kinds of bytestrings: strict (defined in Data.Bytestring.Internal) and lazy (defined in Data.Bytestring.Lazy.Internal). zlib uses lazy bytestrings, as you've discovered.

这篇关于许多类型的字符串(ByteString)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆