Tuple ExamplesUse tuples to store multiple variables together. Return a tuple from a function.
This page was last reviewed on Jan 3, 2023.
Tuple. In a Scala tuple we can combine 2 values in a single unit. We can combine a string and an integer with some simple syntax. The tuple is a single variable.
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.
First example. 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.
Detail We can access the first and second items from a tuple with the "_1" and "_2" syntax.
Note The first item in a tuple is at index 1, not index 0, and we must use "_1" to get it.
// 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
Return multiple values. 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.
Note The upperName def receives a name String and returns a two-item tuple containing the uppercase name and the original name.
// This def returns a two-item tuple. // ... It uppercases the argument String. def upperName(name: String): (String, String) = (name.toUpperCase(), name) // Call upperName function with String. val name = "scala" val result = upperName(name) println(result)
Unpack. 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.
Tip This syntax immediately unpacks a tuple. This can make a program simpler to read.
// Return a tuple. def twoNumbers(x: Int): (Int, Int) = (x * 2, x * 4) // 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.
Here The test() method receives a String, Int tuple. If the first item is "bird" the first case is matched.
def test(animal: (String, Int)) = { animal match { case ("bird", size) => println("Bird weighs", size) case (animal, size) => println("Other animal", animal, size) } } // 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 list. With 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.
// 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2023 Sam Allen.