Scala

Scala Seq

A sequence in Scala is defined as an iterable collection. By this, we mean that the elements of a sequence in Scala are always accessed in sequential order or other words, their order is immutable, i.e., it can never be changed. Because of this sequential order, you can directly access any element of a sequence provided that you know the index of the element where it resides. The indexes of a sequence start from “0” and go up till “n-1,” where “n” represents the length of any particular sequence. A wide range of methods available in Scala can specifically be used with Scala sequences. This guide will learn to use Scala sequences on a Ubuntu 20.04 system.

Using the Scala Seq:

We have designed the following four examples to help you understand the basic concepts associated with the sequences in the Scala programming language, such as declaring a sequence, printing its elements on the terminal, accessing a particular element of a sequence, etc. Let us go through these examples to find out how sequences work in the Scala programming language.

Example # 1: Printing the Elements of a Scala Sequence:

In this example, we will just be sharing with you the method of declaring a sequence in Scala and printing its elements on the terminal. We have designed the following Scala script in this regard:

For using the Scala sequence, we have first imported the “scala.collection.immutable._” library. Then, we have defined the driver class with the name “Sequence” by using the “object” keyword. Within this driver class, we have declared our “main()” function in which we have defined an integer sequence. After that, we have initialized this sequence with five integer elements. Then, we have a “println” statement for displaying a message on the terminal. Finally, we used the “foreach” loop to iterate over this integer sequence and print all of its elements on the terminal.

We have used the name of this script with the compilation command below to compile it:

$ scalac Seq.Scala

Then, we have used the name of the driver class with the following execution command to run this script:

$ scala Sequence

The elements of our designed sequence are shown in the image below:

Example # 2: Accessing a Particular Element of a Scala Sequence:

Instead of printing all the elements of a Scala sequence, you might only want to access a particular element of that sequence. Note that you can access the elements of a Scala sequence in the same way as you can access the elements of an array in any other programming language. Moreover, just like the array, the indexing of the Scala sequences also starts from “0”. The following Scala script will demonstrate how you can access a particular element of a Scala sequence:

In this example, we only wanted to access the second element of our sequence. We have used the same integer sequence that we have defined in the example above. Then, for accessing its second element, we have used the “sequence(1)” notation since it refers to the second element of the sequence.

The execution of this Scala script resulted in printing the second element of our sequence on the terminal, as shown in the image below:

Example # 3: Printing the Elements of a Scala Sequence in the Reverse Order:

In our first example, we shared with you the Scala script for printing the elements of a sequence on the terminal. In this example, we will tell you how you can print the elements of a Scala sequence in reverse order. For doing so, you will have to go through the following Scala script:

In this Scala script, we have defined an integer sequence with five different integer values. After that, we have simply printed these elements on the terminal in the same order in which they are placed by using the “foreach” loop. Then, for printing the elements of the very same sequence in the reverse order, we have made use of the “sequence.reverse” notation in which “sequence” refers to the name of our integer sequence and “reverse” refers to the function that will be responsible for reversing the order of the elements of the Scala sequence.

The output of this script is shown below. You can see that the elements of our sequence were first displayed in the correct order and then in the reverse order.

Example # 4: Checking if a Scala Sequence is Empty or not:

You can also check whether a declared Scala sequence is empty or not, i.e. whether it contains any element or not. For finding it out, we have designed the following Scala script:

In this example, we have defined two different integer sequences. We have assigned five integer values to our first sequence, whereas we have intentionally kept our second sequence empty for testing the effectiveness of the “isEmpty” function of the Scala programming language. This function returns a Boolean output, i.e. true or false. Then, we have called the “isEmpty” function one by one for both of our sequences and printed the output of this function on the terminal.

The output of this Scala script shows that our first sequence was not empty, whereas the second sequence was empty since the value returned by the “isEmpty” function for our first sequence is “false” whereas, for the second sequence, it is “true”.

Conclusion:

This article introduced you to yet another data structure of the Scala programming language, i.e. sequence. We began with a brief introduction of sequences to give you a rough idea about this data structure. Then, we walked you through four simple examples for explaining the working of sequences in the Scala programming language. Once you understand all these examples well, you will be able to make the most out of this data structure while using it within your Scala scripts in Ubuntu 20.04.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.