views:

174

answers:

2

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

+16  A: 

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.

Geoff Reedy
@huynhj you've got it right. I can update the answer to make it more clear
Geoff Reedy
@Geoff, 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.
huynhjl
The string example is very confusing.
ziggystar
+3  A: 

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.

Daniel