scala 如何以最佳方式传递元组参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12230698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a tuple argument the best way?
提问by John Threepwood
How to pass a tuple argument the best way ?
如何以最佳方式传递元组参数?
Example:
例子:
def foo(...): (Int, Int) = ...
def bar(a: Int, b: Int) = ...
Now I would like to pass the output of footo bar. This can be achieved with:
现在我想传递的输出foo来bar。这可以通过以下方式实现:
val fooResult = foo(...)
bar(fooResult._1, fooResult._2)
This approach looks a bit ugly, especially when we deal with a n-tuple with n > 2. Also we have to store the result of foo in an extra value, because otherwise foohas to be executed more than once using bar(foo._1, foo._2).
这种方法看起来有点难看,尤其是当我们处理n带有n > 2. 此外,我们必须将 foo 的结果存储在一个额外的值中,否则foo必须使用bar(foo._1, foo._2).
Is there a better way to pass through the tuple as argument ?
有没有更好的方法来传递元组作为参数?
回答by Tomasz Nurkiewicz
There is a special tupledmethod available for every function:
tupled每个函数都有一个特殊的方法:
val bar2 = (bar _).tupled // or Function.tupled(bar _)
bar2takes a tuple of (Int, Int)(same as bararguments). Now you can say:
bar2接受一个元组(Int, Int)(与bar参数相同)。现在你可以说:
bar2(foo())
If your methods were actually functions (notice the valkeyword) the syntax is much more pleasant:
如果您的方法实际上是函数(注意val关键字),则语法更令人愉快:
val bar = (a: Int, b: Int) => //...
bar.tupled(foo())
See also
也可以看看
回答by dhg
Using tupled, as @Tomasz mentions, is a good approach.
使用tupled,如@Tomasz提到,是一个不错的办法。
You could also extract the tuple returned from fooduring assignment:
您还可以提取foo分配期间返回的元组:
val (x, y) = foo(5)
bar(x, y)
This has the benefit of cleaner code (no _1and _2), and lets you assign descriptive names for xand y, making your code easier to read.
这样做的好处是代码更简洁(no_1和_2),并允许您为x和分配描述性名称y,使您的代码更易于阅读。
回答by Rex Kerr
It is worth also knowing about
也值得了解
foo(...) match { case (a,b) => bar(a,b) }
as an alternative that doesn't require you to explicitly create a temporary fooResult. It's a good compromise when speed and lack of clutter are both important. You can create a function with bar _and then convert it to take a single tuple argument with .tupled, but this creates a two new function objects each time you call the pair; you could store the result, but that could clutter your code unnecessarily.
作为不需要您显式创建临时fooResult. 当速度和避免杂乱都很重要时,这是一个很好的折衷方案。您可以使用 with 创建一个函数,bar _然后将其转换为接受单个元组参数 with .tupled,但是每次调用该对时都会创建两个新的函数对象;您可以存储结果,但这可能会使您的代码不必要地混乱。
For everyday use (i.e. this is not the performance-limiting part of your code), you can just
对于日常使用(即这不是代码的性能限制部分),您可以
(bar _).tupled(foo(...))
in line. Sure, you create two extra function objects, but you most likely just created the tuple also, so you don't care thatmuch, right?
排队。当然,您可以创建两个额外的功能对象,但最有可能刚刚创建的元组也,所以你不在乎那个了吧?

![scala 将 Option[Any] 转换为 int](/res/img/loading.gif)