Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

scala - Stream vs Views vs Iterators

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...