View creates a lazy collection, so calls like filter do not evaluate each element of the collection. Elements are evaluated only after their direct access. sum now accesses all elements, but with view calling filter does not create a full vector. (See Comment from Steve)
A good example of using a view would be:
scala> (1 to 1000000000).filter(_ % 2 == 0).take(10).toList java.lang.OutOfMemoryError: GC overhead limit exceeded
Here Scala is trying to create a collection with 1000000000 items to access the first 10. But with a view:
scala> (1 to 1000000000).view.filter(_ % 2 == 0).take(10).toList res2: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
Kim Stebel Jul 23 '11 at 10:19 2011-07-23 10:19
source share