7

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

  • 1
    What is so special about `Stream` that makes you ask this question? Why shouldn't one use it in production code? – Madoc Mar 19 '11 at 16:07
  • 1
    It's easy to exhaust memory for one. Also I seem to recall reading that its implementation is showing its age. So I'm just curious whether people really use it for things beyond the Euler Project. ;) – Eric Bowman - abstracto - Mar 19 '11 at 16:10

5 Answers5

4

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.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
3

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...

Community
  • 1
  • 1
huynhjl
  • 41,520
  • 14
  • 105
  • 158
3

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.

Michael
  • 10,185
  • 12
  • 59
  • 110
2

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

oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • I guess what motivates me is the realization that Stream is so fragile in terms of memory use, that I would shudder to find it in production code, unless there was no other way. – Eric Bowman - abstracto - Mar 19 '11 at 20:34
1

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.

RM.
  • 1,052
  • 9
  • 9