139

What are the differences among Streams, Views (SeqView), and Iterators in scala? This is my understanding:

  • They are all lazy lists.
  • Streams cache the values.
  • Iterators can only be used once? You can't go back to the beginning and evaluate the value again?
  • View's values are not cached but you can evaluate them again and again?

So if I want to save heap space, should I use iterators (if I won't traverse the list again) or views? Thanks.

1

1 Answer 1

184

First, they are all non-strict. That has a particular mathematical meaning related to functions, but, basically, means they are computed on-demand instead of in advance.

Stream is a lazy list indeed. In fact, in Scala, a Stream is a List whose tail is a lazy val. Once computed, a value stays computed and is reused. Or, as you say, the values are cached.

An Iterator can only be used once because it is a traversal pointer into a collection, and not a collection in itself. What makes it special in Scala is the fact that you can apply transformation such as map and filter and simply get a new Iterator which will only apply these transformations when you ask for the next element.

Scala used to provide iterators which could be reset, but that is very hard to support in a general manner, and they didn't make version 2.8.0.

Views are meant to be viewed much like a database view. It is a series of transformation which one applies to a collection to produce a "virtual" collection. As you said, all transformations are re-applied each time you need to fetch elements from it.

Both Iterator and views have excellent memory characteristics. Stream is nice, but, in Scala, its main benefit is writing infinite sequences (particularly sequences recursively defined). One can avoid keeping all of the Stream in memory, though, by making sure you don't keep a reference to its head (for example, by using def instead of val to define the Stream).

Because of the penalties incurred by views, one should usually force it after applying the transformations, or keep it as a view if only few elements are expected to ever be fetched, compared to the total size of the view.

10
  • 11
    Iterator is also pretty handy for probing the infinite, and I generally prefer them over streams where possible. The real benefit in streams is that previously accessed values are cached, which is a serious boon when trying to implement something like the fibonacci sequence - which is defined in terms of previous values. Mar 1, 2011 at 20:46
  • 6
    Fibonacci is a less than perfect example as it only needs the last 2 previous values, and keeping the whole stream is a waste. The Ackermann function is probably the canonical example. Sep 20, 2012 at 21:56
  • 5
    @JürgenStrobel Ackermann would result in lousy performance, since indexed access of streams is O(n). But I agree wrt fibonacci. Sep 22, 2012 at 21:48
  • 10
    Oh right. This makes Stream a poor choice for any caching approach. Sep 24, 2012 at 7:29
  • 8
    This answer is super clear, it should be part of the documentation... oh, actually it is! Thanks Daniel docs.scala-lang.org/tutorials/FAQ/stream-view-iterator.html
    – Svend
    Jul 25, 2014 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.