What is the difference between a view and a stream?
Asked Answered
C

3

25

In the Scala 2.8 collections framework, what is the difference between view and toStream?

Confluence answered 17/2, 2010 at 17:8 Comment(0)
Y
46

In a view elements are recomputed each time they are accessed. In a stream elements are retained as they are evaluated.

For example:

val doubled = List(1,2,3,4,5,6,7,8,9,10).view.map(_*2)
println(doubled.mkString(" "))
println(doubled.mkString(" "))

will re-evaluate the map for each element twice. Once for the first println, and again for the second. In contrast

val doubled = List(1,2,3,4,5,6,7,8,9,10).toStream.map(_*2)
println(doubled.mkString(" "))
println(doubled.mkString(" "))

will only double the elements once.

A view is like a recipe to create a collection. When you ask for elements of a view it carries out the recipe each time.

A stream is like a guy with a bunch of dry-erase cards. The guy knows how to compute subsequent elements of the collection. You can ask him for the next element of the collection and gives you a card with the element written on it and a string tied from the card to his finger (to help him remember). Also, before he gives you a card he unties the first string from his finger and ties it to the new card.

If you hold onto the first card (i.e. keep a reference to the head of the stream) you might eventually run out of cards (i.e. memory) when you ask for the next element, but if you don't need to go back to the first elements you can cut the string and hand the unneeded cards back to the guy and he can re-use them (they're dry-erase afterall). This is how a stream can represent an infinite sequence without running out of memory.

Yukikoyukio answered 17/2, 2010 at 17:25 Comment(3)
@huynhj you've got it right. I can update the answer to make it more clearYukikoyukio
you can leave the answer as-is. Sometimes a metaphor helps. In this case I got confused by it. The very first sentence had it all.Kassel
The string example is very confusing.Restoration
B
10

Geoff's answer covers almost everything, but I want to add that a Stream is a List-like sequence, while every kind of collections (maps, sets, indexed seqs) have views.

Belt answered 17/2, 2010 at 21:50 Comment(1)
I have one doubt. since function is pure. Why compiler does not use of referential transparency or memoization ? Why view is recomputing it again?Thriftless
L
0

Another way to explain this if you know apache spark would be that using stream is like caching the spark dataset, whereas using view is like using an uncached dataset, meaning that every time you call some action on it, it will reevaluate everything in the DAG.

Lysimachus answered 3/9, 2022 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.