Scala Tutorial - Learn How To Use Scala's Immutable Stream

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:41 pm

Overview

In this tutorial, we will learn how to use Scala's Immutable Stream to perform common operations such as initialize an infinite Stream, use the cons operator, take elements from a Stream and create an empty Stream.

 

More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Immutable Stream will be discussed in Chapter 8 on Collection Functions.

 

What is a Stream?

As per the Scala Documentation, a Stream is a similar data structure to a list except that the elements of the Stream will be lazily computer. As a result, you can have infinitely long Streams!

 

Steps

1. How to create a Stream with 3 numbers using #::

The code below shows how to create a Stream with 3 numbers using #::.


println("Step 1: How to create a Stream with 3 numbers using #::")
val stream1: Stream[Int] = 1 #:: 2 #:: 3 #:: Stream.empty
println(s"Elements of stream1 = $stream1")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to create a Stream with 3 numbers using #::
Elements of stream1 = Stream(1, ?)

NOTE:

  • When we tried to print the elements of the Stream, only the first element of the Stream was printed!
  • As mentioned earlier, the elements of Streams are lazily computed.

2. How to create a Stream with 3 numbers using Stream.cons

The code below shows how to create a Stream with 3 numbers using Stream.cons.


import scala.collection.immutable.Stream.cons
println("\nStep 2: How to create a Stream with 3 numbers using Stream.cons")
val stream2: Stream[Int] = cons(1, cons(2, cons(3, Stream.empty) ) )
println(s"Elements of stream2 = ${stream2}")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to create a Stream with 3 numbers using Stream.cons
Elements of stream2 = Stream(1, ?)

NOTE:

  • Similar to Step 1 above, only the first element of the Stream was printed.
  • Stream.cons is commonly known and referred to as the cons operator.

3. How to print all 3 numbers from stream2 using the take function

The code below shows how to print all 3 numbers from stream2 using the take function.


println("\nStep 3: How to print all 3 numbers from stream2 using the take function")
print("Take first 3 numbers from stream2 = ")
stream2.take(3).print

print("\nTake first 10 numbers from stream2 = ")
stream2.take(10).print

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to print all 3 numbers from stream2 using the take function
Take first 3 numbers from stream2 = 1, 2, 3, empty
Take first 10 numbers from stream2 = 1, 2, 3, empty

NOTE:

  • When we tried to take 10 numbers from the Stream, although it only contained 3 elements, it did not throw any IndexOutOfBoundsException.

4. How to define an infinite Stream of numbers using Stream.cons

The code below shows how to define an infinite Stream of numbers using Stream.cons.


println("\n\nStep 4: How to define an infinite Stream of numbers using Stream.cons")
def inifiniteNumberStream(number: Int): Stream[Int] = Stream.cons(number, inifiniteNumberStream(number + 1))
print("Take only the first 20 numbers from the infinite number stream = ")
inifiniteNumberStream(1).take(20).print

You should see the following output when you run your Scala application in IntelliJ:

Step 4: How to define an infinite Stream of numbers using Stream.cons
Take only the first 20 numbers from the infinite number stream = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, empty

 

5. How to define an infinite stream of numbers using Stream.from

The code below shows how to define an infinite stream of numbers using Stream.from.


println("\n\nStep 5: How to define an infinite stream of numbers using Stream.from")
val stream3: Stream[Int] = Stream.from(1)
print("Take only the first 20 numbers from the infinite number stream = ")
stream3.take(20).print

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to define an infinite stream of numbers using Stream.from
Take only the first 20 numbers from the infinite number stream = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, empty

 

6. How to initialize an empty Stream

The code below shows how to initialize an empty Stream.


println("\n\nStep 6: How to initialize an empty Stream")
val emptyStream: Stream[Int] = Stream.empty[Int]
println(s"Empty Stream = $emptyStream")

You should see the following output when you run your Scala application in IntelliJ:


Step 6: How to initialize an empty Stream
Empty Stream = Stream()

This concludes our tutorial on Learn How To Use Scala's Immutable Stream and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to create a Stream using #::
  • How to create a Stream using Stream.cons
  • How to print elements from Stream using the take function
  • How to define an infinite Stream of numbers using Stream.cons
  • How to define an infinite Stream of numbers using Stream.from
  • How to initialize an empty Stream

Tip

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use Scala's Immutable Vector.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on