从斯卡拉一个迭代的传递函数的参数? [英] pass function arguments from a iterable in Scala?
本文介绍了从斯卡拉一个迭代的传递函数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能从一个迭代的传递函数的参数在Scala呢?
Is it possible to pass function arguments from a iterable in Scala?
val arguments= List(1,2)
def mysum(a:Int,b:Int)={a+b}
如何将一个呼叫mysum使用列表中的内容作为参数?
How would one call mysum using the List contents as arguments?
推荐答案
有关它在列表工作,你将需要以适应 mysum
函数可变参数:
For it to work on lists you'll need to accomodate the mysum
function to "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
这篇关于从斯卡拉一个迭代的传递函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文