Scala Tutorial - Learn How To Use Flatten Function
Overview
In this tutorial, we will learn how to use the flatten function on collection data structures in Scala. The flatten function is applicable to both Scala's Mutable and Immutable collection data structures.
The flatten method will collapse the elements of a collection to create a single collection with elements of the same type.
As per the Scala documentation, the definition of the flatten method is as follows:
def flatten[B]: Traversable[B]
The flatten method is a member of the GenericTraversableTemplate trait but there are specialized versions of the flatten methods for given collection types.
Steps
1. How to initialize a Sequence of donuts
The code below shows how to initialize a Sequence of Donut elements of type String.
println("Step 1: How to initialize a Sequence of donuts")
val donuts1: Seq[String] = Seq("Plain", "Strawberry", "Glazed")
println(s"Elements of donuts1 = $donuts1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a Sequence of donuts
Elements of donuts1 = List(Plain, Strawberry, Glazed)
2. How to initialize another Sequence of donuts
The code below shows how to initialize another Sequence of Donut elements of type String .
println("\nStep 2: How to initialize another Sequence of donuts")
val donuts2: Seq[String] = Seq("Vanilla", "Glazed")
println(s"Elements of donuts2 = $donuts2")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to initialize another Sequence of donuts
Elements of donuts2 = List(Vanilla, Glazed)
3. How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2
The code below shows how to create a List of donut elements and initialize the List by using the two donut sequences from Step 1 and Step 2 respectively.
println("\nStep 3: How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2")
val listDonuts: List[Seq[String]] = List(donuts1, donuts2)
println(s"Elements of listDonuts = $listDonuts")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2
Elements of listDonuts = List(List(Plain, Strawberry, Glazed), List(Vanilla, Glazed))
NOTE:
- We end up with a List containing two inner lists: List(List(Plain, Strawberry, Glazed), List(Vanilla, Glazed))
- What if you need to produce a single List of donut elements as opposed to having a List with two inner lists? We can achieve this using the flatten method as shown in Step 4 below.
4. How to return a single list of donut using the flatten function
The code below shows how to use the flatten method on the List of donuts which contains two inner donut lists and return a single donut List of type String.
println("\nStep 4: How to return a single list of donut using the flatten function")
val listDonutsFromFlatten: List[String] = listDonuts.flatten
println(s"Elements of listDonutsFromFlatten = $listDonutsFromFlatten")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to return a single list of donut using the flatten function
Elements of listDonutsFromFlatten = List(Plain, Strawberry, Glazed, Vanilla, Glazed)
NOTE:
- By using the flatten method, we were able to collapse the donut String elements into a single list.
5. How to append the word Donut to each element of listDonuts using flatten and map functions
The code below shows how to use the flatten method followed by the map method to produce a single List of donut elements where the word Donut was appended to each element.
println("\nStep 5: How to append the word Donut to each element of listDonuts using flatten and map functions")
val listDonutsFromFlatten2: List[String] = listDonuts.flatten.map(_ + " Donut")
println(s"Elements of listDonutsFromFlatten2 = $listDonutsFromFlatten2")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to append the word Donut to each element of listDonuts using flatten and map functions
Elements of listDonutsFromFlatten2 = List(Plain Donut, Strawberry Donut, Glazed Donut, Vanilla Donut, Glazed Donut)
Summary
In this tutorial, we went over the following:
- How to initialize a Sequence of donuts
- How to initialize another Sequence of donuts
- How to create a List of donuts initialized using the two Sequences from Step 1 and Step 2
- How to return a single list of donut using the flatten function
- How to append the word Donut to each element of listDonuts using flatten and map functions
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 fold functions.