Scala Tutorial - Learn How To Use ScanRight Function With Examples

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

Overview

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

 

The scanRight 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 right to left and hence the name scanRight.  Similar to the foldRight method, scanRight allows you to also specify an initial value.

 

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

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

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

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

 

NOTE:

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

scanRight method iterations
5 + 4 + 3 + 2 + 1 = 15
5 + 4 + 3 + 2     = 14
5 + 4 + 3         = 12
5 + 4             =  9
5 + 0             =  5
0                 =  0

 

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

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


println("\nStep 3: How to create a running total using the scanRight function explicitly")
val runningTotal2: Seq[Int] = numbers.scanRight(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 scanRight function explicitly
Running total of all elements in the collection = List(15, 14, 12, 9, 5, 0)

NOTE:

  • Similar to Step 2, the iterations shown below should help you visualize each iteration of the scanRight method.
scanRight method iterations
5 + 4 + 3 + 2 + 1 = 15
5 + 4 + 3 + 2     = 14
5 + 4 + 3         = 12
5 + 4             = 9
5 + 0             = 5
0                 = 0

 

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