Scala Tutorial - Learn How To Use FoldLeft Function With Examples
Overview
In this tutorial, we will learn how to use the foldLeft function with examples on collection data structures in Scala. The foldLeft function is applicable to both Scala's Mutable and Immutable collection data structures.
The foldLeft 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 left to right and hence the name foldLeft. The foldLeft method allows you to also specify an initial value.
Using foldLeft is fundamental in recursive function and will help you prevent stack-overflow exceptions.
As per the Scala documentation, the definition of the foldLeft method is as follows:
def foldLeft[B](z: B)(op: (B, A) ⇒ B): B
The foldLeft method 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 foldLeft function
The code below shows how to sum all the donut prices from the sequence by using the foldLeft method.
println("\nStep 2: How to sum all the donut prices using foldLeft function")
val sum = prices.foldLeft(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 foldLeft function
Sum = 6.0
NOTE:
- Note that we passed in a default or initial value of 0.0 to the foldLeft 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 foldLeft 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 foldLeft method.
println("\nStep 4: How to create a String of all donuts using foldLeft function")
println(s"All donuts = ${donuts.foldLeft("")((a, b) => a + b + " Donut ")}")
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 foldLeft function
All donuts = Plain Donut Strawberry Donut Glazed Donut
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 + b + " Donut "
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>
6. How to create a String of all donuts using value function from Step 5 and foldLeft 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 foldLeft method.
println("\nStep 6: How to create a String of all donuts using value function from Step 5 and foldLeft function")
println(s"All donuts = ${donuts.foldLeft("")(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 foldLeft 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 foldLeft function
- How to initialize a Sequence of donuts
- How to create a String of all donuts using foldLeft 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 foldLeft 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 foldRight function.