生产代码中的流式传输
人们真的在生产代码中使用 Scala 的 Stream 类吗?或者它主要是出于学术兴趣?
Do people really use Scala's Stream class in production code, or is it primarily of academic interest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Stream
没有问题,除非人们用它来替换Iterator
——而不是替换List
,后者是最类似于的集合它。在这种特殊情况下,人们在使用时必须小心。另一方面,使用迭代器也必须小心,因为每个元素只能迭代一次。那么,既然两者都有自己的问题,为什么要单独挑选
Stream
呢?我敢说这只是人们习惯了 Java 中的 Iterator,而 Stream 是一个函数式的东西。There's no problem with
Stream
, except when people use it to replaceIterator
-- as opposed to replacingList
, 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 usingIterator
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 toIterator
from Java, whereasStream
is a functional thing.即使我写了迭代器是我的几乎所有时间都想使用我确实在生产代码中使用
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...请查看此处。这篇博文介绍了如何使用 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.是的,我使用它,尽管它往往用于类似这样的事情:
当然,在此示例中我可能会使用非严格视图和
find
Yes, I use it, although it tends to be for something like this:
Of course, I might use a non-strict view and a
find
for this example由于不使用 Stream 的唯一原因是确保 JVM 不会保留对早期 conses 的引用可能很棘手,因此我使用过的一种相当不错的方法是构建一个
Stream
并立即将其转换为Iterator
以供实际使用。它在使用方面失去了一些Stream
的良好属性,尤其是在回溯方面,但如果您只想对结果进行一次传递,那么以这种方式构建结构通常会更容易而不是直接扭曲成Iterator
的hasNext
/next()
模型。Since the only reason not to use
Stream
s 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 aStream
and immediately convert it to anIterator
for actual use. It loses a little ofStream
'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 thehasNext
/next()
model ofIterator
directly.