生产代码中的流式传输

发布于 2024-10-24 07:55:29 字数 51 浏览 2 评论 0原文

人们真的在生产代码中使用 Scala 的 Stream 类吗?或者它主要是出于学术兴趣?

Do people really use Scala's Stream class in production code, or is it primarily of academic interest?

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

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

发布评论

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

评论(5

似梦非梦 2024-10-31 07:55:29

Stream 没有问题,除非人们用它来替换 Iterator ——而不是替换 List,后者是最类似于的集合它。在这种特殊情况下,人们在使用时必须小心。另一方面,使用迭代器也必须小心,因为每个元素只能迭代一次。

那么,既然两者都有自己的问题,为什么要单独挑选 Stream 呢?我敢说这只是人们习惯了 Java 中的 Iterator,而 Stream 是一个函数式的东西。

There's no problem with Stream, except when people use it to replace Iterator -- as opposed to replacing List, which is the collection most similar to it. In that particular case, one has to be careful in its use. On the other hand, one has to be careful using Iterator as well, since each element can only be iterated through once.

So, since both have their own problems, why single out Stream's? I daresay it's simply that people are used to Iterator from Java, whereas Stream is a functional thing.

江湖彼岸 2024-10-31 07:55:29

即使我写了迭代器是我的几乎所有时间都想使用我确实在生产代码中使用Stream。我只是不会自动假设单元格已被垃圾收集。

有时 Stream 可以完美地解决这个问题。我认为 api 提供了一些很好的例子涉及递归的地方...

Even though I wrote that Iterator is what I want to use nearly all the time I do use Stream in production code. I just don't automatically assume that the cells are garbage collected.

Sometimes Stream fits the problem perfectly. I think the api gives some good examples where recursion is involved...

热血少△年 2024-10-31 07:55:29

请查看此处。这篇博文介绍了如何使用 Scala Streams(以及内存映射文件)高效读取大文件 (1-2G)。

我还没有尝试过,但解决方案看起来很合理。 Stream 在低级 ByteBuffer Java API 之上提供了一个很好的抽象,用于将内存映射文件作为记录序列进行处理。

Look here. This blog post describes how to use Scala Streams (along with memory mapped file) to read large files (1-2G) efficiently.

I did not try it yet but the solution looks reasonable. Stream provides a nice abstraction on top of the low level ByteBuffer Java API to handle a memory mapped file as a sequence of records.

感情废物 2024-10-31 07:55:29

是的,我使用它,尽管它往往用于类似这样的事情:

(as.toStream collect expensiveConversionToB) match {
  case b #:: _ => //found my expensive b
  case _       =>
}

当然,在此示例中我可能会使用非严格视图和 find

Yes, I use it, although it tends to be for something like this:

(as.toStream collect expensiveConversionToB) match {
  case b #:: _ => //found my expensive b
  case _       =>
}

Of course, I might use a non-strict view and a find for this example

ゞ花落谁相伴 2024-10-31 07:55:29

由于不使用 Stream 的唯一原因是确保 JVM 不会保留对早期 conses 的引用可能很棘手,因此我使用过的一种相当不错的方法是构建一个Stream 并立即将其转换为 Iterator 以供实际使用。它在使用方面失去了一些 Stream 的良好属性,尤其是在回溯方面,但如果您只想对结果进行一次传递,那么以这种方式构建结构通常会更容易而不是直接扭曲成 IteratorhasNext/next() 模型。

Since the only reason not to use Streams is that it can be tricky to ensure the JVM isn't keeping references to early conses around, one approach I've used that's fairly nice is to build up a Stream and immediately convert it to an Iterator for actual use. It loses a little of Stream's nice properties on the use side especially with respect to backtracking, but if you're only going to make one pass over the result it's frequently easier to build a structure this way than to contort into the hasNext/next() model of Iterator directly.

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