在 Haskell 中,对 Lazy ByteString 调用 length 会强制将整个字符串放入内存吗?

发布于 2024-08-26 17:58:34 字数 254 浏览 5 评论 0原文

我正在使用惰性字节串读取大数据流,并且想知道在解析它时是否至少有 X 个字节可用。也就是说,我想知道字节串是否至少有 X 个字节长。

对其调用 length 是否会导致整个流被加载,从而违背使用惰性字节串的目的?

如果是,那么后续操作将是:如何在不加载整个流的情况下判断它是否至少有 X 个字节?

编辑:最初我在读取文件的上下文中询问,但了解有更好的方法来确定文件大小。然而,我需要的最终解决方案不应依赖于惰性字节串源。

I am reading a large data stream using lazy bytestrings, and want to know if at least X more bytes is available while parsing it. That is, I want to know if the bytestring is at least X bytes long.

Will calling length on it result in the entire stream getting loaded, hence defeating the purpose of using the lazy bytestring?

If yes, then the followup would be: How to tell if it has at least X bytes without loading the entire stream?

EDIT: Originally I asked in the context of reading files but understand that there are better ways to determine filesize. Te ultimate solution I need however should not depend on the lazy bytestring source.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野侃 2024-09-02 17:58:35

它将必须迭代整个字符串,但如果您不在其他任何地方保留对整个惰性字节字符串的引用,我相信它应该能够在字符串向尾部前进时释放字符串的头部。

It will have to iterate the entire string, but if you don't keep a reference to the entire lazy byte-string anywhere else, I believe it should be able to free the head of the string as it progresses towards its tail.

忱杏 2024-09-02 17:58:34

是的。

<代码>长度。取x。

Yes.

length . take x.

一个人的旅程 2024-09-02 17:58:34

您不使用 hFileSize :: Handle -> 有什么原因吗? IO Integer 用于获取文件的长度?

Is there a reason you're not using hFileSize :: Handle -> IO Integer for getting the length of the file?

紙鸢 2024-09-02 17:58:34

编辑:抱歉。我想我认为字节串是列表。字节串没有 genericLength。

length 是严格的,因为它返回的 Int 类型是严格的。您可以使用 Data.List 中的 genericLength 并导入一个定义惰性 Peano 数字的库,并为其提供一个 Num 实例,例如 numbers 库:

这可以让您以您想要的方式表达您的功能,但 ehemient 的答案在功能上是相同的,并且不需要导入新的库。

我刚刚在这里发表了一篇关于该主题的博客文章,如果这听起来像您可能感兴趣的方法:

http://coder.bsimmons.name/blog/2010/03/lazy-arithmetic-in-haskell/

EDIT: sorry. I guess I was thinking bytestrings were lists. There is no genericLength for bytestrings.

length is strict because the type it returns Int is strict. You can use genericLength from Data.List and import a library that defines lazy Peano numbers and gives you a Num instance for them, e.g the numbers library:

That would let you express your function in the way you would like, but ephemient's answer is functionally the same, and doesn't require importing a new library.

I just did a blog post on the subject here, if that sounds like an approach you might be interested in:

http://coder.bsimmons.name/blog/2010/03/lazy-arithmetic-in-haskell/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文