Home
Map
Tuple ExamplesUse tuples to store multiple variables together. Return a tuple from a function.
Scala
This page was last reviewed on Dec 12, 2023.
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.
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.
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
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.
def
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)
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.
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.
match
Here The 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 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.
List
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
Summary. 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.
This page was last updated on Dec 12, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.