Scala Tutorial - Learn How To Use Reverse Function With Examples

By Nadim Bahadoor | Last updated: November 30, 2017 at 20:59 pm

Overview

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

 

The reverse method will create a new sequence with the elements in reversed order.

 

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

def reverse: Repr

The reverse method is a member of SeqLike trait.

Steps

1. How to initialize a sequence of donut prices

The code below show how to initialize a Sequence of type Double to represent donut prices..

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 get the elements of the sequence in reverse using the reverse method

The code below shows how to use the reverse method get back a Sequence where the elements from the original collection have been reversed.


println("\nStep 2: How to get the elements of the sequence in reverse using the reverse method")
println(s"Elements of donuts in reversed order = ${donuts.reverse}")

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


Step 2: How to get the elements of the sequence in reverse using the reverse method
Elements of donuts in reversed order = List(Glazed Donut, Strawberry Donut, Plain Donut)

 

3. How to access each reversed element using reverse and foreach methods

To iterate through the reversed elements in a collection, you can make use of the familiar foreach method.


println("\nStep 3: How to access each reversed element using reverse and foreach methods")
donuts.reverse.foreach(donut => println(s"donut = $donut"))

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


Step 3: How to access each reversed element using reverse and foreach methods
donut = Glazed Donut
donut = Strawberry Donut
donut = Plain Donut

 

This concludes our tutorial on Learn How To Use Reverse 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 donut prices
  • How to get the elements of the sequence in reverse using the reverse method
  • How to access each reversed element using reverse and foreach methods

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