简体   繁体   English

将多参数匿名函数作为参数的Scala函数

[英]Scala function that takes a multiple parameter anonymous function, as a parameter

I have a function named sum that can take a single parameter anonymous function as a parameter, and two Integers. 我有一个名为sum的函数,可以将一个参数匿名函数作为参数,以及两个Integer。

def sum (f: Int => Int , a: Int, b: Int): Int =
{
  if(a > b) 0 else f(a) + sum(f, a + 1, b)
}
  sum((x: Int) => x , 2, 10)

How could I modify the function definition so that it can take a multiple parameter function, so I could call it like this: 我如何修改函数定义,以便可以使用多参数函数,所以可以这样称呼它:

sum((y: Int, i: Int) => y + i => x , 2, 10)

I know the function I have supplied would be pretty useless when passed a multiple parameter function.. but I am just looking for how it can be done.. 我知道我提供的函数在传递多参数函数时会毫无用处,但是我只是在寻找如何完成它。

Thanks 谢谢

As simple as this: 就这么简单:

def sum (f: (Int, Int) => Int , a: Int, b: Int): Int = ???

Or a curried version: 或咖喱版本:

def sum (f: Int => Int => Int , a: Int, b: Int): Int = ???

Although you can curry any function just calling f.curried 尽管您可以咖喱任何函数,只需调用f.curried

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM