【问题标题】:Why this method currying can run in such a way为什么这种方法currying可以以这种方式运行
【发布时间】:2021-07-19 13:30:14
【问题描述】:
我对下面的代码感到困惑。为什么这个可以运行? .isEmpty 是什么意思。在调用test的过程中,我很神秘{是什么意思?新功能?
def test(age:Int)(name:name_class => name_class): String = {
val str: String = name.toString()+age+""
print(str)
str
}
test(19){
x =>
name_class("dage1")
}.isEmpty
【问题讨论】:
标签:
scala
functional-programming
【解决方案1】:
-
def test - test 是一种方法。
-
(age:Int) - 它需要一个 Int 参数。我们称之为age。
-
(name:name_class => name_class) - 它采用第二个参数(curried),这是一个接受 name_class 并返回 name_class 的函数。我们将调用该函数name。
-
: String - 方法 test 返回 String。
...
-
test(19) - 让我们调用 test 方法。第一个参数 age 将是 19。
-
{ - test 的第二个参数从这里开始。它可能是多行代码,所以我们将使用{...} 而不是(...)。 (在这种情况下实际上不需要。)
-
x=> - 这是一个接受 name_class 的函数。我们称之为x。 (以后忽略它。)
-
name_class("dage1") - 返回 name_class。
-
} - 全部通过 test 的第二个参数完成。
-
.isEmpty - 测试test 返回的String 是否为空。