简体   繁体   English

Scala中f(a,b)和f(a)(b)之间的差异

[英]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.

相关问题 将&#39;A =&gt; F [G [B]]&#39;转换为&#39;F [G [A =&gt; B]&#39;标量 - transform 'A => F[G[B]]' into 'F[G[A=>B]' scala 如何将F [A \\ / B]拆分为(F [A],F [B]) - How to split F[A \/ B] into (F[A], F[B]) Scala中A <:B和+ B之间有什么区别? - What's the difference between A<:B and +B in Scala? def compose [A,B,C](f:B =&gt; C,g:A =&gt; B):A =&gt; C = {f(g(_))}是不是有效的Scala声明? - def compose[A,B,C](f: B => C, g: A => B): A => C = {f(g(_))} is noty valid scala declaration? 如何将F [Either [A,B]]转换为Either [F [A],F [B]] - How to transform F[Either[A, B]] to Either[F[A], F[B]] F[_] 和 F[T] 在 Scala 中用于类型构造函数时的区别 - Difference between F[_] and F[T] In Scala when used in type constructors xs映射(f)和xs之间的差异。 斯卡拉地图(f) - difference between xs map (f) and xs . map (f) in Scala `F[_ &lt;: A] &lt;: B` 在类型级别和 `f: A =&gt; B` 在值级别之间的类比 - Analogy between `F[_ <: A] <: B` at type-level and `f: A => B` at value-level Scala中Array.map(f:A =&gt; B)的性能不佳 - Poor performance of Array.map(f: A => B) in Scala Scala如何将Seq [(((A,B,C,D),Seq [(E,F,G)])]]转换为Seq [(A,B,C,D,Seq [(E,F,G) ])]? - Scala How to transform Seq[((A, B, C, D), Seq[(E, F, G)])] to Seq[(A, B, C, D, Seq[(E, F, G)])]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM