Scala, Using Pattern Match to Deconstruct a Tuple

Deconstructing a tuple in Scala:

scala> val t = (5, 3)
t: (Int, Int) = (5,3)

scala> val (a: Int, b: Int) = t
a: Int = 5
b: Int = 3

scala> val (a: Int, _) = t
a: Int = 5

It is even possble to deconstruct the Tuple as an argument to a function:

scala> def f: Tuple2[Int, Int] => Int = t => a*b
f: ((Int, Int)) => Int

scala> f(t)
res3: Int = 15

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s