[英]Difference between f(a,b) and f(a)(b) in Scala
I am very very new to Scala. 我对Scala非常新。 I am reading a book called functional programming in scala by Paul Chiusano and Rúnar Bjarnason.
我正在阅读Paul Chiusano和RúnarBjarnason在scala中编写的一本名为函数式编程的书。 So far I am finding it interesting.
到目前为止,我发现它很有趣。 I see a solution for curry and uncurry
我看到咖喱和不干扰的解决方案
def curry[A,B,C](f: (A, B) => C): A => (B => C)= {
a => b => f(a,b)
}
def uncurry[A,B,C](f: A => B => C): (A, B) => C = {
(a,b) => f(a)(b)
}
In Curry I understand f(a,b) which results in value of type C but in uncurry I do not understand f(a)(b). 在库里,我理解f(a,b)导致C型的值,但是在不合理的情况下,我不理解f(a)(b)。 Can anyone please tell me how to read f(a)(b) or how is this resulting to a type of C or please refer me some online material that can explain this to me?
任何人都可以告诉我如何阅读f(a)(b)或如何得到一种C或请参考我一些可以向我解释的在线资料?
Thanks for your help. 谢谢你的帮助。
Basically the return type of f(a)
is a function of type B => C
lets call this result g
. 基本上
f(a)
的返回类型是B => C
的函数,我们可以调用这个结果g
。 If you then call g(b)
you obtain a value of type C
. 如果然后调用
g(b)
,则获得类型C
的值。 f(a)(b)
can be expanded to f.apply(a).apply(b)
f(a)(b)
可以扩展为f.apply(a).apply(b)
In the uncurry
method you take a so-called "curried" function, meaning that instead of having a function that evaluates n arguments, you have n functions evaluating one argument, each returning a new function until you evaluate the final one. 在
uncurry
方法中,你采用了一个所谓的“curried”函数,这意味着你没有一个函数来评估n个参数,你有n个函数来评估一个参数,每个函数返回一个新函数,直到你评估最后一个参数。
Currying without a specific support from the language mean you have to do something like this: 在没有语言特定支持的情况下进行干扰意味着您必须执行以下操作:
// curriedSum is a function that takes an integer,
// which returns a function that takes an integer
// and returns the sum of the two
def curriedSum(a: Int): Int => Int =
b => a + b
Scala however provides further support for currying, allowing you to write this: 然而,Scala为currying提供了进一步的支持,允许你写这个:
def curriedSum(a: Int)(b: Int): Int = a + b
In both cases, you can partially apply curriedSum
, getting a function that takes an integer and sums it to the number you passed in originally, like this: 在这两种情况下,您都可以部分应用
curriedSum
,获取一个取整数的函数,并将其与最初传入的数相加,如下所示:
val sumTwo: Int => Int = curriedSum(2)
val four = sumTwo(2) // four equals 4
Let's go back to your case: as we mentioned, uncurry
takes a curried function and turns it into a regular function, meaning that 让我们回到你的情况:当我们提到,
uncurry
需要一个咖喱功能,并把它变成一个普通的功能,也就是说,
f(a)(b)
can read as: "apply parameter a
to the function f
, then take the resulting function and apply the parameter b
to it". 可以读作:“将参数
a
应用于函数f
,然后获取结果函数并将参数b
应用于它”。
In case if somebody is looking for an explanation. 如果有人正在寻找解释。 This link explains it better
此链接更好地解释了它
def add(x:Int, y:Int) = x + y
add(1, 2) // 3
add(7, 3) // 10
After currying 干嘛
def add(x:Int) = (y:Int) => x + y
add(1)(2) // 3
add(7)(3) // 10
In the first sample, the add method takes two parameters and returns the result of adding the two. 在第一个示例中,add方法接受两个参数并返回添加两个参数的结果。 The second sample redefines the add method so that it takes only a single Int as a parameter and returns a functional (closure) as a result.
第二个示例重新定义了add方法,这样它只需要一个Int作为参数,并返回一个函数(闭包)作为结果。 Our driver code then calls this functional, passing the second “parameter”.
然后我们的驱动程序代码调用此函数,传递第二个“参数”。 This functional computes the value and returns the final result.
此函数计算值并返回最终结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.