如何在现有的 Haskell 代码中从 String 转换为 Data.ByteString.Lazy?
我有一个使用大量字符串的 Haskell 代码,在对其进行分析时,似乎该代码使用大量内存来存储列表 []。解决这个问题的一种方法是使用 Data.ByteString.Lazy 而不是 String,但是
这样做时我必须关心什么?
代码的哪一部分必须仔细查看:fold,map,...?
感谢您的回复
I have a Haskell code which use a lot of String, while profilling it, it appear that the code use a lot of memory to store Lists []. One solution to this problem is to use Data.ByteString.Lazy instead of String, but
what I have to care about while doing this ?,
which part of the code have to be look carefully : fold, map, ... ?
thanks for reply
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该知道,
ByteString
对于元素迭代之类的事情确实很糟糕,但对于串联等来说却更好。如果您想使用 ByteStrings,则必须将 String 转换为 ByteString,只需执行类似操作
,在每个与其配合使用的函数前面粘贴一个
B
即可 -String
的大多数函数也适用于ByteString
。请注意 - 您必须使用某些函数将您使用的字符串转换为 ByteString。如果您使用
Data.ByteString.Lazy.Char8
代替,则可以轻松使用pack
,但所有大于 255 的字符都将被截断。此外,这种类型更适合二进制数据并且安全内存。编辑:如果您想处理文本字符串,您应该考虑使用包文本。请参阅此处了解更多详细信息。
You should know, that a
ByteString
is really bad for things like iteration over it elements, but better for Concatation, etc.If you want to work with ByteStrings, you have to convert the String to a ByteString, just do something like
and stick a
B
in front of each function which works with them - most functions forString
also exists forByteString
. Please notice - you have to convert the Strings you use to a ByteString with some functions.If you use
Data.ByteString.Lazy.Char8
instead, you can easily usepack
, but all chars greater than 255 will be truncated. Also, this type is more suitable for binary data and safes memory.Edit: You should consider using the package text, if you want to work on text-strings. Look here for further details.
OverloadedStrings 扩展程序如果您使用 GHC 并使用大量字符串文字转换代码,这会很方便。只需将以下内容添加到源文件的顶部:
并且您不必在代码中的任何字符串文字上使用
B.pack
。例如,您可以有以下内容:如果没有扩展名,这将产生错误,因为您不能在
ByteString
和[Char ]
。通过扩展,字符串文字的类型为(IsString a) =>; a
,而ByteString
是IsString
的实例,因此这里的"Test"
被输入为ByteString并且没有错误。
The OverloadedStrings extension can be handy if you're using GHC and are converting code with a lot of string literals. Just add the following to the top of your source file:
And you don't have to use
B.pack
on any string literals in your code. You could have the following, for example:Without the extension this would give an error, since you can't use
==
on aByteString
and a[Char]
. With the extension, string literals have type(IsString a) => a
, andByteString
is an instance ofIsString
, so"Test"
here is typed as aByteString
and there's no error.