Scala Tutorial - Learn How To Use FoldRight Function With Examples
Overview
In this tutorial, we will learn how to use the foldRight function with examples on collection data structures in Scala. The foldRight function is applicable to both Scala's Mutable and Immutable collection data structures.
The foldRight method takes an associative binary operator function as parameter and will use it to collapse elements from the collection. The order for traversing the elements in the collection is from right to left and hence the name foldRight. The foldRight method allows you to also specify an initial value.
Prefer using foldLeft as opposed to foldRight since foldLeft is fundamental in recursive function and will help you prevent stack-overflow exceptions.
As per the Scala documentation, the definition of the foldRight method is as follows:
def foldRight[B](z: B)(op: (A, B) ⇒ B): B
The foldRightmethod is a member of the TraversableOnce trait.
Steps
1. How to initialize a sequence of donut prices
The code below shows how to initialize a sequence of donut prices where each element in the sequence is of type Double.
println("Step 1: How to initialize a sequence of donut prices")
val prices: Seq[Double] = Seq(1.5, 2.0, 2.5)
println(s"Donut prices = $prices")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a sequence of donut prices
Donut prices = List(1.5, 2.0, 2.5)
2. How to sum all the donut prices using foldRight function
The code below shows how to sum all the donut prices from the sequence by using the foldRight method.
println("\nStep 2: How to sum all the donut prices using foldRight function")
val sum = prices.foldRight(0.0)(_ + _)
println(s"Sum = $sum")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to sum all the donut prices using foldRight function
Sum = 6.0
NOTE:
- Note that we passed in a default or initial value of 0.0 to the foldRight method because the sequence of Donuts was of type Double.
3. How to initialize a Sequence of donuts
The code below shows how to initialize a Sequence of donuts where each element is of type String.
println("\nStep 3: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain", "Strawberry", "Glazed")
println(s"Elements of donuts1 = $donuts")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to initialize a Sequence of donuts
Elements of donuts1 = List(Plain, Strawberry, Glazed)
4. How to create a String of all donuts using foldRight function
The code below shows how to create a String representation for all the donut elements in the Sequence and also append the word Donut to each element using the foldRight method.
println("\nStep 4: How to create a String of all donuts using foldRight function")
println(s"All donuts = ${donuts.foldRight("")((a, b) => a + " Donut " + b)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to create a String of all donuts using foldRight function
All donuts = Plain Donut Strawberry Donut Glazed Donut
NOTE:
- The foldRight implementation is different from foldLeft which was a + b + " Donut " from the previous tutorial.
5. How to declare a value function to create the donut string
The code below shows how to declare a Value Function to encapsulate the behaviour for creating a String representation for all donut elements.
println("\nStep 5: How to declare a value function to create the donut string")
val concatDonuts: (String, String) => String = (a, b) => a + " Donut " + b
println(s"Value function concatDonuts = $concatDonuts")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to declare a value function to create the donut string
Value function concatDonuts = <function2>
NOTE:
- Similar to Step 4, the foldRight implementation is different from foldLeft which was a + b + " Donut " from the previous tutorial.
6. How to create a String of all donuts using value function from Step 5 and foldRight function
The code below shows how to create a String representation for the donut elements by passing through the Value Function from Step 5 to the foldRight method.
println("\nStep 6: How to create a String of all donuts using value function from Step 5 and foldRight function")
println(s"All donuts = ${donuts.foldRight("")(concatDonuts)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 6: How to create a String of all donuts using value function from Step 5 and foldRight function
All donuts = Plain Donut Strawberry Donut Glazed Donut
Summary
In this tutorial, we went over the following:
- How to initialize a sequence of donut prices
- How to sum all the donut prices using foldRight function
- How to initialize a Sequence of donuts
- How to create a String of all donuts using foldRight function
- How to declare a value function to create the donut string
- How to create a String of all donuts using value function from Step 5 and foldRight function
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 foreach function.