In python I can do this:
def f((a, b)):
return a + b
d = (1, 2)
f(d)
Here the passed in tuple is being decomposed while its being passed to f.
Right now in scala I am doing this:
def f(ab: (Int, Int)): Int = {
val (a, b) = ab
a + b
}
val d = (1, 2)
f(d)
Is there something I can do here so that the decomposition happens while the arguments are passed in? Just curious.
You can create a function and match its input with pattern matching:
Or match the input of the method with the
matchkeyword:Another way is to use a function with two arguments and to “tuple” it: