To deal with excessive copying, we can resort to a feature called deferred processing, also known as, lazy collections. A collection is lazy when all of its elements are not realized at the time of creation. Instead, elements are computed on demand.
Let's write a program to generate numbers from
100. We wish to check which numbers are evenly divisible by
Let's generate a lazy collection of the input numbers:
scala> val list = (1 to 100).toList.view list: scala.collection.SeqView[Int,List[Int]] = SeqView(...)
We convert an existing Scala collection into a lazy one by calling the
view function. Note that the list elements are not printed out, as these are not yet computed.
The following snippet shows ...