Computer scienceProgramming languagesScalaTypes and data structuresSpecial types

Tuples

4 minutes read

You know some basic Scala collections, including Maps. As you remember, a map is a collection of key-value pairs. A pair, in its turn, is a special data structure called a tuple. In this topic, we will learn more about this type of language expression and how it can help us define complex data types.

Pairs

A pair is a basic example of a tuple. In Scala, a tuple is a value that contains a fixed number of elements, where each may have a unique type. The ability to have a variety of types is the main difference between tuples and collections, where elements have to be similar. For example, a pair can be two elements with the key Char and the value Int:

scala> 'a' -> 1
res0: (Char, Int) = (a,1)

Note, that for pairs we use special syntax with an arrow. This is just syntactic sugar that makes it easier to distinguish programming tuples from tuples in mathematics:

scala> val tuple = ('a', 1)
tuple: (Char, Int) = (a,1)

Obviously, you can define any combination of types:

scala> (1.0, "string")
res1: (Double, String) = (1.0,string)
scala> (true, 4f)
res2: (Boolean, Float) = (true,4.0)
scala> (8L, ())
res3: (Long, Unit) = (8,())

After the definition of pair, you can access the first element as _1 and second as _2:

scala> tuple._1
res4: Char = a
scala> tuple._2
res5: Int = 1

Triples

In some cases, combining two elements (key and value) is not enough, so we need three. This is not a problem, we have triples! Imagine that for some app we need the name, the year of birth, and the rating of a person. We can define it like this:

scala> val triple = ("Alex", 1982, 3)
triple: (String, Int, Int) = (Alex,1982,3)

You can access the elements by _1, _2 and _3 respectively:

scala> triple._3
res6: Int = 3

Note that we don't use syntax with an arrow for triples: if we do use it, we define a pair of a pair and element:

scala> val nonTriple = "Alex" -> 1982 -> 3
nonTriple: ((String, Int), Int) = ((Alex,1982),3)

We can keep going and define tuples with more elements: 4, 5, 6... These types don't have special names in Scala like pairs or triples do.

Tuples

Large tuples are similar to database records: we have some data with a type in a place (column). We can represent user data with additional fields and define a collection of tuples:

scala> val userList = List(("Alice", 1985, 9, 4, 'a'), ("Scally", 1988, 5, 6, 's'), ("Bob", 1990, 12, 7, 'b'))
userList: List[(String, Int, Int, Int, Char)] = List((Alice,1985,9,4,a), (Scally,1988,5,6,s), (Bob,1990,12,7,b))
scala> userList(0)._1
res6: String = Alice
scala> userList(2)._5
res7: Char = b

In Scala 2, tuples had a limitation: it was impossible to define more than 22 elements.

scala> (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
<console>:1: error: too many elements for tuple: 23, allowed: 22
       (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
       ^

In Scala 3, on the other hand, Tuple has become a more powerful data structure, which has no such limitation. In the new version of the language, Tuple also has methods, which are similar to collection methods. How they work is an advanced topic, which we won't talk about in this course.

Operation Example Result
size (1, 2, 3).size 3
head (3 *: 4 *: 5 *: EmptyTuple).head 3
tail (3 *: 4 *: 5 *: EmptyTuple).tail (4, 5)
*: 3 *: 4 *: 5 *: 6 *: EmptyTuple (3, 4, 5, 6)
++ (1, 2, 3) ++ (4, 5, 6) (1, 2, 3, 4, 5, 6)
drop (1, 2, 3).drop(2) (3)
take (1, 2, 3).take(2) (1, 2)

It is also worth noting that the apply method can now be used to get a specific element. If a literal is passed as an index, the compiler will check the offered index and the method call will be completely safe and similar to ._1 or ._22

scala> (1, 2, 3)(2) // or (1, 2, 3).apply(2)
res0: Int = 3

If you pass the index through a variable of type Int, the method call may not be safe and will be similar to the apply method on sequences like List.

Conclusion

For tuples, we start with two elements (a pair), because a tuple with one element is the element itself. Three elements form a triplet, and tuples with larger sizes exist without special names. With the help of tuples, we can define structured data of various types: database records, registries, maps, and many others.

13 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo