Scala Tutorial - Learn How To Use Partition Function With Examples

By Nadim Bahadoor | Last updated: November 26, 2017 at 21:50 pm

Overview

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

 

The partition method takes a predicate function as its parameter and will use it to return two collections: one collection with elements that satisfied the predicate function and another collection with elements that did not match the predicate function.

 

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

def partition(p: (A) ⇒ Boolean): (Repr, Repr)

 

The partition method is a member of the TraversableLike trait.

Steps

1. How to initialize a sequence which contains donut names and prices

The code below shows how to declare a Sequence of both type String and Double to represent donut names and prices in a single collection..

println("Step 1: How to initialize a sequence which contains donut names and prices")
val donutNamesAndPrices: Seq[Any] = Seq("Plain Donut", 1.5, "Strawberry Donut", 2.0, "Glazed Donut", 2.5)
println(s"Elements of donutNamesAndPrices = $donutNamesAndPrices")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize a sequence which contains donut names and prices
Elements of donutNamesAndPrices = List(Plain Donut, 1.5, Strawberry Donut, 2.0, Glazed Donut, 2.5)

 

2. How to split the sequence by the element types using partition function

The code below shows how to use the partition method to pattern match on elements of type String and Double in order to create two collections, that is, one for each type..


println("\nStep 2: How to split the sequence by the element types using partition function")
val namesAndPrices: (Seq[Any], Seq[Any]) = donutNamesAndPrices.partition {
  case name: String => true
  case price: Double => false
}
println(s"Elements of namesAndPrices = $namesAndPrices")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to split the sequence by the element types using partition function
Elements of namesAndPrices = (List(Plain Donut, Strawberry Donut, Glazed Donut),List(1.5, 2.0, 2.5))

 

3. How to access the donut String sequence from Step 2

A tuple was created as a result of using the partition method in Step 2. The code below shows how to access the String elements which represent donut names.


println("\nStep 3: How to access the donut String sequence from Step 2")
println(s"Donut names = ${namesAndPrices._1}")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to access the donut String sequence from Step 2
Donut names = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

4. How to access the donut prices sequence from Step 2

In Step 3, we showed how to access the donut names. The code below shows how to access the donut prices collection.


println("\nStep 4: How to access the donut prices sequence from Step 2")
println(s"Donut prices = ${namesAndPrices._2}")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to access the donut prices sequence from Step 2
Donut prices = List(1.5, 2.0, 2.5)

 

5. How to extract the pair returned by partition function

Similar to Step 2, we will once more call the partition method in order to split the original sequence from Step 1. However, in the example below, we will make use of an extractor to assign the tuple from the partition method into two sequences namely donutNames and donutPrices.


println("\nStep 5: How to extract the pair returned by partition function")
val (donutNames, donutPrices) = donutNamesAndPrices.partition {
  case name: String => true
  case _ => false
}
println(s"donutNames = $donutNames")
println(s"donutPrices = $donutPrices")

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to extract the pair returned by partition function
donutNames = List(Plain Donut, Strawberry Donut, Glazed Donut)
donutPrices = List(1.5, 2.0, 2.5)

This concludes our tutorial on Learn How To Use Partition 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 which contains donut names and prices
  • How to split the sequence by the element types using partition function
  • How to access the donut String sequence from Step 2
  • How to access the donut prices sequence from Step 2
  • How to extract the pair returned by partition function

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 reduce 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