I Like Tuples in Scala. I’m almost as happy as when I met Ranges in Groovy/Ruby. My main programming language is Java and there aren’t such nice control structures.
Then, what is Tuple?
Tuple in Scala is part of the Collections API.
Tuple combines a fixed number of items together and all can be used as one object. Tuples can hold objects with different types (unlike an array or list) and are immutable.
What means Tuple is collection with fixed size?
Every Tuple you define is one of the class :Tuple2, Tuple3, …,Tuple22 .The number in name of class is an amount of items this object can hold. Then if you use Tuple3 class, you can hold only 3 items in it. And yes, upper limit is 22 because there is no Tuple23 class in API.
How to use it:
The standard way is new object invocation from class:
1 2 |
val t = new Tuple3(1, false, "hello") |
Example above show us tuple that holds Integer, Boolean and String.
But there are the simplest way. In fact, you no need to invoke a class. You just put all items inside rounded braces:
1 2 |
val t = (1, false, "hello") |
The actual type of a tuple depends upon the number and of elements it contains and the types of those elements. Thus, the type of (99, „none”) is Tuple2[Int, String].
Tuples are collections and you can access any element in it. Scala provides elements-access methods for every tuple class. To access elements of a tuple t, you can use method t._1
to access the first element, t._2
to access the second, and so on.
1 2 |
val sumOfFirstThreeElements = t._1 + t._2 + t._3 |
There is an iterator for iterate over collection. You get it by use Tuple.productIterator()
method.
1 2 3 4 5 6 |
val t = (1,2,3,4,5) t.productIterator.foreach{ println _ } //or val ti = t.productIterator while(ti.hasNext) println(xi.next) |
Convert to String
Tuples override toString()
method to pretty print what it contains:
1 2 3 |
val t = ( 4,3,1,2) t.toString() |
Tuple2
Class Tuple2 has few differences between other tuples. Two are worth few words.
First, most important, is another type of declaration. We can use ->
as separator both elements.
1 2 3 |
scala> 1 -> 2 res0: (Int, Int) = (1,2) |
this method you see new Map is invoked.
1 2 |
Map(1 -> 2) |
Another addition is swap
method.
Tuple2 has a method Tuple2.swap
that swaps elements, for example:
1 2 3 4 |
val s = ("world", "hello") s.swap.toString() scala> (hello,world) |
Remember the swap
method is only in the Tuple2 class.
Where can I use Tuples?
Anywhere! In fact you use tuples when you use Maps. All elements in maps are stored as Tuple2, (example from REPL):
1 2 3 4 5 6 |
scala> val m = Map(1 -> "first element", 2 -> "second element") m: scala.collection.immutable.Map[Int,String] = Map(1 -> first element, 2 -> second element) scala> m.foreach(kv => println(kv.swap)) (first element,1) (second element,2) |
You can use tuples to assign multiple variables at once:
1 2 3 4 5 6 |
scala> val (x, y z) = ( 1, false, "none") scala> val (x , y, z) = ( 1, false, "none") x: Int = 1 y: Boolean = false z: String = none |