Scala Tutorial - Learn How To Use Transpose Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 15:12 pm

Overview

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

 

The transpose method will pair and overlay elements from another collections into a single collection.

 

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

def transpose[B](implicit asTraversable: (A) ⇒ GenTraversableOnce[B]): CC[CC[B]]

The transpose method is a member of GenericTraversableTemplate trait.

Steps

1. How to initialize a Sequence of donuts

The code below shows how to create a List of donuts of type String.


println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")

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 donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

2. How to initialize donut prices

The code below shows how to create a Sequence of donut prices of type Double.


println("\nStep 2: How to initialize donut prices")
val prices: Seq[Double] = Seq(1.50, 2.0, 2.50)
println(s"Elements of prices = $prices")

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


Step 2: How to initialize donut prices
Elements of prices = List(1.5, 2.0, 2.5)

3. How to create a List of donuts and prices

Using the donuts and prices Sequence from Step 1 and Step 2, we can create a single list which will wrap the donuts and prices collection.


println("\nStep 3: How to create a List of donuts and prices")
val donutList = List(donuts, prices)
println(s"Sequence of donuts and prices = $donutList")

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


Step 3: How to create a List of donuts and prices
Sequence of donuts and prices = List(List(Plain Donut, Strawberry Donut, Glazed Donut), List(1.5, 2.0, 2.5))

4. How to pair each element from both donuts and prices Sequences using the transpose function

The issue with Step 3 is that you end up with a nested List structure to represent donuts and their prices. Using the transpose method, we can pair up each element from the donuts and prices collection as shown below.


println("\nStep 4: How to pair each element from both donuts and prices Sequences using the transpose function")
println(s"Transposed list of donuts paired with their individual prices = ${donutList.transpose}")

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


Step 4: How to pair each element from both donuts and prices Sequences using the transpose function
Transposed list of donuts paired with their individual prices = List(List(Plain Donut, 1.5), List(Strawberry Donut, 2.0), List(Glazed Donut, 2.5))

 

This concludes our tutorial on Learn How To Use Transpose 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 of donuts
  • How to initialize donut prices
  • How to create a List of donuts and prices
  • How to pair each element from both donuts and prices Sequences using the transpose 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 the union 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: