Bạn có thể tạo một hàm và phù hợp với đầu vào của nó với mô hình kết hợp:
scala> val f: ((Int, Int)) => Int = { case (a,b) => a+b }
f: ((Int, Int)) => Int
scala> f(1, 2)
res0: Int = 3
Hoặc phù hợp với đầu vào của phương pháp này với match
keyword:
scala> def f(ab: (Int, Int)): Int = ab match { case (a,b) => a+b }
f: (ab: (Int, Int))Int
scala> f(1, 2)
res1: Int = 3
Một cách khác là sử dụng một chức năng với hai đối số và để "tuple" nó:
scala> val f: (Int, Int) => Int = _+_
f: (Int, Int) => Int = <function2>
scala> val g = f.tupled // or Function.tupled(f)
g: ((Int, Int)) => Int = <function1>
scala> g(1, 2)
res10: Int = 3
// or with a method
scala> def f(a: Int, b: Int): Int = a+b
f: (a: Int, b: Int)Int
scala> val g = (f _).tupled // or Function.tupled(f _)
g: ((Int, Int)) => Int = <function1>
scala> g(1, 2)
res11: Int = 3
// or inlined
scala> val f: ((Int,Int)) => Int = Function.tupled(_+_)
f: ((Int, Int)) => Int = <function1>
scala> f(1, 2)
res12: Int = 3
Thú vị. Tôi không biết điều này thậm chí có thể bằng Python. – Debilski
Ngoài ra: https://issues.scala-lang.org/browse/SI-813 – Debilski