Scala Tutorial - Learn How To Use Par Function With Examples

By Nadim Bahadoor | Last updated: February 5, 2020 at 12:41 pm

Overview

In this tutorial, we will learn how to use the par function with examples on collection data structures in Scala. The par function is applicable to both Scala's Mutable and Immutable collection data structures. This tutorial is for Scala versions prior to Scala 2.13.0. As of Scala 2.13.0, there has been some structural changes to the par function, and you can use our par function tutorial for Scala 2.13.0 and above for further details.

 

The par method on collection provides a very easy high level API to allow computation to run in parallel to take advantage of multi-core processing. When you call the par method on a collection, it will copy all the elements into an equivalent Scala Parallel Collection. For additional information on Scala's Parallel Collection, see the official documentation.

 

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

def par: ParRepr

 

The par method is a member of the Parallelizable trait.

Steps

1. How to initialize an Immutable Sequence of various donut flavours

The code below shows how to initialize an Immutable Sequence of donut flaours of type String.

println("Step 1: How to initialize an Immutable Sequence of various donut flavours")
val donutFlavours: Seq[String] = Seq("Plain", "Strawberry", "Glazed")
println(s"Elements of donutFlavours immutable sequence = $donutFlavours")

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


Step 1: How to initialize an Immutable Sequence of various donut flavours
Elements of donutFlavours immutable sequence = List(Plain, Strawberry, Glazed)

 

2. Convert the Immutable donut flavours Sequence into Parallel Collection

The code below shows how to call the par method on the Immutable Sequence to create an equivalent Parallel Collection of type String.


println("\nStep 2: Convert the Immutable donut flavours Sequence into Parallel Collection")
import scala.collection.parallel.ParSeq
val donutFlavoursParallel: ParSeq[String] = donutFlavours.par

 

NOTE:

  • We added the import scala.collection.parallel.ParSeq in order to have the ParSeq in scope.

3. How to use Scala Parallel Collection

The code below shows how to use the Scala Parallel Collection of donut flavours from Step 2 and append the String "donut" to each of the element using the map method.


println("\nStep 3: How to use Scala Parallel Collection")
val donuts: ParSeq[String] = donutFlavoursParallel.map(d => s"$d donut")
println(s"Elements of donuts parallel collection = $donuts")

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


Step 3: Use Scala Parallel Collection
Elements of donuts parallel collection = ParVector(Plain donut, Strawberry donut, Glazed donut)

 

 

This concludes our tutorial on Learn How To Use Par 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 an Immutable Sequence of various donut flavours
  • Convert the Immutable donut flavours Sequence into Parallel Collection
  • How to use Scala Parallel Collection

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 partition 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: