O'Reilly logo

Learning Functional Data Structures and Algorithms by Raju Kumar Mishra, Atul Khot

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Streams

Scala's streams are lazy lists, an infinite sequence of elements. For example, just like we zip two lists together, we could zip two streams too:

scala> def zip(p: Stream[Int], q: Stream[Int]): Stream[(Int, Int)] = 
     | (p.head, q.head) #:: zip(p.tail, q.tail) 
zip: (p: Stream[Int], q: Stream[Int])Stream[(Int, Int)] 
 
scala> val r = zip(Stream.from(9), Stream.from(10)) 
r: Stream[(Int, Int)] = Stream((9,10), ?) 

As you can see, the type of r variable is another stream. Also, the stream has a head element, a pair (9,10), and a function (also called a thunk). The function (in this case, zip) is called to produce successive elements.

As you can see, the tail is a question, meaning it is not yet computed; therefore, it is not yet known.

Why are streams ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required