Scala Tuples with Scala Tuple type and Examples

1. Scala Tuples

What are Scala Tuples? How do we iterate over them? How do we convert tuples in Scala to strings or swap their elements? These are the questions we will answer in this article.

Scala Tuples

Scala Tuples

2. What are Scala Tuples?

Scala tuple holds together a fixed number of items so we can pass them around as a whole. Unlike a list or an array, we can put different types of objects in it; they are also immutable. Let’s take an example.

scala> val t=(1,"Ayushi",Console)
t: (Int, String, Console.type) = (1,Ayushi,scala.Console$@7210f559)
Note that this is syntactic sugar for this:
scala> val t=new Tuple3(1,"Ayushi",Console)
t: (Int, String, Console.type) = (1,Ayushi,scala.Console$@7210f559)
You can also declare a tuple the following way:
scala> 1->'a'
res0: (Int, Char) = (1,a)
scala> 1->'a'->'b'
res1: ((Int, Char), Char) = ((1,a),b)

We can also use Scala tuples to pass a list of data values as messages between actors in concurrent programming.

3. Scala tuple type and How to Access Them

The number of elements in a Scala tuple and their types decides a tuple’s overall type. So, let’s see some types. The type for (7,”Ayushi”) is Tuple2(Int, String). That for (“Red”,”Golden”,’o’,7,’g’,4,”Ayushi”) is Tuple6(String, String, Char, Int, Char, Int, String). This is how Scala decides the types of tuples- as Tuple1, Tuple2, Tuple3, and so on. 1<=N<=22. Scala will currently limit you to 22 members in a tuple, but you may use a collection instead if you want more. For a TupleN type, Scala gives us some methods to access the elements.

Let’s take the tuple t we defined earlier. To access the string “Ayushi” from it, we type the following onto the console:

scala> t._2
res0: String = Ayushi

Note that here, the indexing doesn’t begin at 1 for once. If we try to access an index that doesn’t exist in the tuple, it raises a console error:

scala> t._4
<console>:13: error: value _4 is not a member of (Int, String, Console.type)

4. Scala Tuples Example

Before moving forward, let’s take an example to demonstrate how we use tuples.

object tuples extends App{
    val t=(1,2,3)
    val mult=t._1*t._2*t._3
    println("These multiply to "+mult)

We save this as tuples.scala on our Desktop, then exit Scala and move to the Desktop on the command prompt.

scala> Terminate batch job (Y/N)? y
C:\Users\lifei>cd Desktop
C:\Users\lifei\Desktop>scalac tuples.scala
C:\Users\lifei\Desktop>scala tuples

These multiply to 6

As you can see, this prints the product of all three members of the tuple.

5. Iterating Over a Tuple in Scala

To iterate over an entire tuple, we use the method productIterator().

object tuples extends App{

    val t=(1,2,3)
And this is what we do in the command prompt:
C:\Users\lifei\Desktop>scalac tuples.scala
C:\Users\lifei\Desktop>scala tuples




6. Converting a Scala Tuple to String

Wait, what exactly does this mean? Well, converting a tuple to a string concatenates all of its elements into a string. We use the toString() method for this.

object tuples extends App{
    val t=("Hello"," ","World")
And in the command prompt:
C:\Users\lifei\Desktop>scalac tuples.scala
C:\Users\lifei\Desktop>scala tuples
(Hello, ,World)

7. Swapping Elements

Finally, let’s discuss how we can swap elements in a tuple. This will allow us to exchange positions of elements in a Tuple2 type.

object tuples extends App{
    val t=("Hello","World")
The command prompt shows:
C:\Users\lifei\Desktop>scalac tuples.scala
C:\Users\lifei\Desktop>scala tuples

It swapped the two values to each other’s positions.

8. Conclusion

This is all about tuples in Scala. Let us know if anything confuses you. And remember to practice.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *