Streams
A stream is generally a lazy and linear sequence collection. Stream elements are evaluated only when needed. Streams are already defined in Scala and Clojure. Since elements of a stream are evaluated lazily, it can be of infinite length. In this section, we will explore streams in Scala and Clojure. Let me start with stream in Scala. List, which we are going to explore in the coming chapter, differs from stream only in lazy computation. Other properties of list are similar to stream.
Stream in Scala
Stream is a class defined in Scala. Stream constructor can be used to create Stream object. Stream elements are evaluated when they are required. Stream-calculated values are cached so that they can be used further. Let us explorer the characteristics of streams in Scala:
scala> import scala.Stream import scala.Stream scala> var ms = Stream("a","b","c","d","e") ms: scala.collection.immutable.Stream[String] = Stream(a, ?...