Scala Tutorial - Learn How To Use ScanLeft Function With Examples

By Nadim Bahadoor | Last updated: December 5, 2017 at 21:18 pm

Overview

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

 

The scanLeft method takes an associative binary operator function as parameter and will use it to collapse elements from the collection to create a running total. The order for traversing the elements in the collection is from left to right and hence the name scanLeft.  Similar to the foldLeft method, scanLeft allows you to also specify an initial value.

 

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

def scanLeft[B, That](z: B)(op: (B, A) ⇒ B)(implicit bf: CanBuildFrom[Repr, B, That]): That

The scanLeft 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 scanLeft 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 scanLeft function

The code below shows how to use the scanLeft 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 scanLeft function")
val runningTotal: Seq[Int] = numbers.scanLeft(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 scanLeft function
Running total of all elements in the collection = List(0, 1, 3, 6, 10, 15)

 

NOTE:

  • If you've never used the scanLeft 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 scanLeft method.

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

 

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

Similar to the example in Step 2, we will show how to use the scanLeft 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 scanLeft method.


println("\nStep 3: How to create a running total using the scanLeft function explicitly")
val runningTotal2: Seq[Int] = numbers.scanLeft(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 scanLeft 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 scanLeft method.

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

 

This concludes our tutorial on Learn How To Use ScanLeft 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 scanLeft function
  • How to create a running total using the scanLeft 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 scanRight function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Principal Engineer | 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