function - Scala 函数定义参数列表上的不同括号样式

标签 function scala functional-programming higher-order-functions

Scala 中以下两个函数定义的区别是什么:

1) def sum(f: Int => Int)(a: Int, b: Int): Int = { <code removed> }
2) def sum(f: Int => Int, a: Int, b: Int): Int = { <code removed> }
?

SBT 的控制台 REPL 为它们提供了不同的值(value),所以看看它们是否有什么不同:
sum: (f: Int => Int, a: Int, b: Int)Intsum: (f: Int => Int)(a: Int, b: Int)Int

最佳答案

第一个定义是 curried ,以便您提供ab在另一个时间。

例如,如果您知道要在当前方法中使用的函数,但还不知道参数,则可以这样使用它:

def mySum(v: Int): Int = v + 1
val newsum = sum(mySum) _

此时,newsum是一个需要两个 Int 的函数s 并返回 Int .

在总结的背景下,它似乎没有多大意义;然而,有很多次我想根据我现在知道的东西为程序的各个部分返回不同的算法,但还不知道(或无法访问)参数。

Currying 为您提供了该功能。

关于function - Scala 函数定义参数列表上的不同括号样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19036682/

相关文章:

javascript - 如何限制 javascript/Angular 中第一个位置的空间

r - 如何编写函数来计算 R 中的 H 指数?

Scala 模式与 list 匹配

oop - 向面向对象的程序员和技术含量较低的人解释函数式编程

function - Excel IF 语句中的范围

java - 如何使用这种方法在 Java 中构建二叉树?

scala - 迭代循环方式

scala - 尝试创建 scala jar 时出现下载失败错误

javascript - 函数式编程: How to convert an impure function to a pure function when the input needs to be mutated

javascript - 如何共享延续的中间结果?