Tuple
In Scala 3.3, a tuple combines 2 or more values in a single unit. For example, we can combine a string
and an integer with some simple syntax.
With tuples, we build abstractions over related values. This helps simplify many parts of our Scala programs. A function can return more than 1 value in a tuple.
Here we create 2 tuples, each with 2 items. The tuples have a String
and Int
. We can access the first and second items in them.
object Program { def main(args: Array[String]): Unit = { // Create and print two tuples. val identity1 = ("ABC", 10) val identity2 = ("DEF", 20) println(identity1) println(identity2) // Get first and second items from a tuple. val first = identity1._1 val second = identity1._2 println(first) println(second) } }(ABC,10) (DEF,20) ABC 10
Often in programs we want to return multiple values from a function. Parameters, classes, or tuples may be used. Here we return a tuple from function.
upperName
def
receives a name String
and returns a two-item tuple containing the uppercase name and the original name.object Program { // This def returns a two-item tuple. // ... It uppercases the argument String. def upperName(name: String): (String, String) = (name.toUpperCase(), name) def main(args: Array[String]): Unit = { // Call upperName function with String. val name = "scala" val result = upperName(name) println(result) } }(SCALA,scala)
We can unpack a tuple in an assignment statement. Here the function returns a tuple with two items in it. We assign the values number1 and number2 to those items.
object Program { // Return a tuple. def twoNumbers(x: Int): (Int, Int) = (x * 2, x * 4) def main(args: Array[String]): Unit = { // Unpack the tuple returned by the function. val (number1, number2) = twoNumbers(3) println(number1) println(number2) } }6 12
Match
We can use a tuple as the argument of a match construct. We specify values within match cases to require specific values in a tuple.
test()
method receives a String
, Int
tuple. If the first item is "bird" the first case is matched.object Program { def test(animal: (String, Int)) = { animal match { case ("bird", size) => println("Bird weighs " + size) case (animal, size) => println("Other animal " + animal + ", " + size) } } def main(args: Array[String]): Unit = { // Call test method with tuple argument. val animal = ("bird", 20) test(animal) val animal2 = ("cat", 300) test(animal2) } }Bird weighs 20 Other animal cat, 300
Tuple
listWith a list of tuples we can make a 2D array or a collection of complex elements. Here we use 3 items in each tuple and access them in a for
-loop.
object Program { def main(args: Array[String]): Unit = { // Create list of tuples. val animals = List(("cat", 10, "yellow"), ("bird", 1, "blue")) // Loop over tuples. for (value <- animals) { println(value._1 + " is " + value._3) println("... weighs " + value._2) } } }cat is yellow ... weighs 10 bird is blue ... weighs 1
Tuples are common in Scala. They have a simple syntax form, so we can use them with minimal effort. They combine data together, enabling manipulation of data units.