Scala Tutorial - Learn How To Use Scan Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 15:05 pm

Overview

In this tutorial, we will learn how to use the scan function with examples on collection data structures in Scala. The scan function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The scan method takes an associative binary operator function as parameter and will use it to collapse elements from the collection to create a running total from each element. Similar to the fold method, scan allows you to also specify an initial value.

 

As per the Scala documentation, the definition of the scan method is as follows:

def scan[B >: A, That](z: B)(op: (B, B) ⇒ B)(implicit cbf: CanBuildFrom[Repr, B, That]): That

The scan method is a member of TraversableLike trait.

Steps

1. How to initialize a sequence of numbers

The code below show how to initialize a Sequence of numbers. Note that we're not using our donuts examples similar to previous tutorials because it will be easier to visualize each iteration of the scan method.


println("Step 1: How to initialize a sequence of numbers")
val numbers: Seq[Int] = Seq(1, 2, 3, 4, 5)
println(s"Elements of numbers = $numbers")

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


Step 1: How to initialize a sequence of numbers
Elements of numbers = List(1, 2, 3, 4, 5)

 

2. How to create a running total using the scan function

The code below shows how to use the scan method to create a running total from all the elements in the numbers Sequence of Step 1.


println("\nStep 2: How to create a running total using the scan function")
val runningTotal: Seq[Int] = numbers.scan(0)(_ + _)
println(s"Running total of all elements in the collection = $runningTotal")

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


Step 2: How to create a running total using the scan function
Running total of all elements in the collection = List(0, 1, 3, 6, 10, 15)

 

NOTE:

  • If you've never used the scan method in the past, it may not be obvious on how it works. The iteration below should hopefully help you visualize the inner working of the scan method.

Scan method iterations
0 + 1             =  1
1 + 2             =  3
1 + 2 + 3         =  6
1 + 2 + 3 + 4     = 10
1 + 2 + 3 + 4 + 5 = 15

 

3. How to create a running total using the scan function explicitly

Similar to the example in Step 2, we will show how to use the scan method to create a running total from the numbers Sequence in Step 1. However, in the code below, we will be more explicit with the operator function we pass through to the scan method.


println("\nStep 3: How to create a running total using the scan function explicitly")
val runningTotal2: Seq[Int] = numbers.scan(0)((a, b) => a + b)
println(s"Running total of all elements in the collection = $runningTotal2")

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


Step 3: How to create a running total using the scan function explicitly
Running total of all elements in the collection = List(0, 1, 3, 6, 10, 15)

NOTE:

  • Similar to Step 2, the iterations shown below should help you visualize each iteration of the scan method.

Scan method iterations
0 + 1             =  1
1 + 2             =  3
1 + 2 + 3         =  6
1 + 2 + 3 + 4     = 10
1 + 2 + 3 + 4 + 5 = 15

 

This concludes our tutorial on Learn How To Use Scan Function With Examples 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 initialize a sequence of numbers
  • How to create a running total using the scan function
  • How to create a running total using the scan function explicitly

Tip

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

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 the scanLeft function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: