Scala

Scala Vector

Scala is itself a very diverse language when it comes to different collections or packages. One of its collections is the “Vector” collection which can be utilized to access random values in the form of a list. Using the vectors, we can manipulate random values of lists using different functions of vectors. It enables persistent time access to the values, allowing for simple access plus modification thanks to using a vector. Within this tutorial on Scala programming, we will discuss the creation and modification of vectors using simple Vector functions of Scala programming. So make sure to have Scala configured on Ubuntu 20.04.

Create Vectors

Before jumping to the implementation of Scala code in a Scala file, we recommend you use the Scala command line to create vectors and manipulate them accordingly to understand more. So we tried the “scala” keyword in the query area to open the Scala shell as displayed.

To create a single vector “vec”, you need to utilize the “Vector” function along with some values in its brackets. As the output shows, we added the Integer values for this vector:

The same way can be used to create a String type vector “v”, as demonstrated below:

Empty Vector
To create an empty vector “x”, you need to leave brackets “()” empty as shown below:

Using the “toVector” Function
You can also create a vector using the toVector() function of Scala programming by specifying the start and end point of an integer range using the “to” keyword in the brackets, i.e., from 2 to 7. The output shows that the vector has been created starting from 2 and ending at 7.

Using “range” Function
You can utilize the range() function to build a new vector with a specific range, i.e., 6 to 10. The endpoint would be excluded from the output vector, as displayed:

Using the “fill” Function
The “fill” function can let you create a vector by repeating a single value as many times as you want, i.e., we have been making a vector with the “scala” element repeated twice.

Using “tabulate” Function
The tabulate function allows you to create a vector of specified size by letting us specify the first element. The rest will be updated by itself, i.e., the starting point is 3, and a total of 5 consecutive elements have been added to this vector.

Vector Append

You can append a new element in an already built vector. We have created a vector “x” with 5 elements and created a new vector “y” using the “x” vector and appending 12 at its end.

Within the append method, we can also utilize the Seq() function to append multiple numbers in a vector as we did in the following image, i.e., add 13 and 15 at the end of vector “x” and save to “y”:

Vector Prepend

Here, you can append the value at the start of any vector, i.e., by specifying it before the “+” sign in the following line of code:

Distinct Vector Elements

You might add some duplicate values in a vector, but you don’t want to display them as it is. So, you want a distinct function only to display them once. Thus, we have created a vector “v” with many duplicate values and called the distinct function to display the duplicate values once.

Drop Vector Elements

You can also drop the vector elements while displaying them without complete removal. For this, you can use the “drop” function with a total number of elements to be dropped from the start of a vector “v”.

Vector Head and Tail

You can find out the first element of a vector “v” using the head function, i.e., as the output shows the value 1 in return. Also, you can show the tail of the vector “v” using the tail function as displayed, i.e., all elements except the first.

 

Vector Map

The map function of Scala can be applied to all elements of a vector “v” to get a new sequence via a specific number, i.e., we have been multiplying vector “v” by a number 3 using the “8” sign.

 

Vector Reverse

The reverse function of vector collection can be cast-off to reverse the order of a vector list. Thus, we have tried this reverse() function on vector “v” and got the reversed vector.

 

Vector Union

The union function can combine two vectors in one, i.e., as we have combined vectors “x” and “y” in the following output:

Vector Empty and Size

The “isEmpty” function can inform you whether the particular vector is empty or not by showing a Boolean result. And the size function of vectors can tell you the exact size of a vector.

Example

We will be using an example of Scala to elaborate vectors more clearly. This time, we will be creating a string vector. Thus, we have created a Scala file in Ubuntu 20.04 and imported the immutable collection of Scala at its first line. After that, we created a test class object containing a main() function definition. We have created two string vectors, v1 and v2, including 2 and 1 values, respectively.

The foreach loop has been used to print elements of the v1 and v2 vectors. We performed a merge between the v1 and v2 vectors using the “++” sign to create a vector v3. We added the “to Scala” element at the end of a vector v3 using the “:+” sign to create another vector v4 and, in the end, sorted the v4 vector.

We have compiled the test.scala file using the “scalac” compiler and executed its object file “test” using the “scala” instruction. The output displays vector v1 and v2 separately, merging v3 of v1 and v2, adding “to Scala” to v3 as v4, and sort v5.

Conclusion

We started this article’s implementation by defining the vectors and explaining their use in Scala programming. After that, we utilized the Scala command line to create vectors, merge vectors, append and prepend values to vectors, and perform different functions on the vectors. All the examples were explained using the integer type vectors. We have concluded this article using a simple Scala program containing the creation and manipulation of string vectors.

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.