将元组传递给函数参数 [英] Passing tuples to function parameters

查看:42
本文介绍了将元组传递给函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个代码:

def a(x:Int,y:Int):Int = x+y

def b:(Int,Int) = (1,2)

我想完成:

  a(b)

这样做的正确方法是什么?是否还有更有效的方法来调用预定义的多参数函数 - 在我的例子 8 - 中使用另一个函数的结果?

What is the proper way of doing this? Also are there more efficient ways of calling a predefined multi-parameter function - in my case 8 - with the results of another function?

推荐答案

怎么样:

scala> (a _).tupled(b)
res0: Int = 3

  • a 是一种方法.a _ 给你一个部分应用的函数.
  • Function2.tupled 根据您的函数创建一个元组版本.
    • a is a method. a _ gives you a partially applied function.
    • Function2.tupled creates a tupled version from your function.
    • 这篇关于将元组传递给函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆