scala 函数中嵌套函数
Scala中的咖喱函数 (Currying function in Scala)
A currying function is a transforming function with multiple arguments transformed into single arguments. A currying function takes two arguments into a function that takes only a single argument.
currying函数是将多个参数转换为单个参数的转换函数 。 一个currying函数将两个参数合并为仅包含一个参数的函数。
There are two syntaxes to define the currying functions in Scala.
在Scala中有两种语法来定义currying函数 。
Syntax:
句法:
def functionName(arg1) = (arg2) => operation
def functionName(arg1) (arg2) = operation
Syntax explanation:
语法说明:
In the first syntax, the function takes arg1 which equals arg2 and then the operation is performed.
在第一个语法中,函数采用ARG1它等于ARG2,然后执行该操作。
The first single argument is the original function argument. This function returns another function that takes the second of the original function. This chaining continuous for all arguments of the function.
第一个参数是原始函数参数。 此函数返回另一个函数,该函数采用原始函数的第二个。 该链接对于函数的所有自变量都是连续的。
The last function in this chain does the actual word of the function call.
该链中的最后一个函数执行函数调用的实际字。
Example:
例:
object MyClass {
def add(x: Int) (y: Int) = x + y;
def main(args: Array[String]) {
println("sum of x + y = " + add(25)(10) );
println("sum of a + b = " + add(214)(4564) );
}
}
Output
输出量
sum of x + y = 35
sum of a + b = 4778
Code explanation:
代码说明:
The above code defines and uses a currying function named add this function simply adds two numbers and return their addition. But is defined based on how a currying function is defined. The call also sends two numbers like two different functions in the function call.
上面的代码定义并使用了一个名为add的循环函数,此函数简单地将两个数字相加并返回它们的加法。 但是根据如何定义递归函数来定义。 该调用还会发送两个数字,就像函数调用中的两个不同函数一样。
Currying is a little bit tricky concept and you need to properly understand it to master over this. But this concept is useful while programming some big programs in Scala.
咖喱是一个有点棘手的概念,您需要适当地理解它才能掌握它。 但是,在使用Scala编写一些大型程序时,此概念很有用。
翻译自: https://www.includehelp.com/scala/currying-function-in-scala.aspx
scala 函数中嵌套函数