Arguments of argument function from iterable to Scala?

Is it possible to pass arguments to a function from an iteration in Scala?

val arguments= List(1,2) def mysum(a:Int,b:Int)={a+b} 

How to call mysum using List contents as arguments?

+4
source share
1 answer

For it to work on lists, you need to place the mysum function for "varargs":

 scala> def mysum ( args : Int* ) = args.sum mysum: (args: Int*)Int scala> val arguments = List(1,2) arguments: List[Int] = List(1, 2) scala> mysum(arguments: _*) res0: Int = 3 
+3
source

All Articles