我试图了解函数部分应用在 Scala 中的工作原理。
为此,我构建了这个简单的代码:
object Test extends App {
myCustomConcat("General", "Public", "License") foreach print
GeneralPublicLicenceAcronym(myCustomConcat(_)) foreach print
def myCustomConcat(strings: String*): List[Char] = {
val result = for (s <- strings) yield {
s.charAt(0)
}
result.toList
}
def GeneralPublicLicenceAcronym (concatFunction: (String*) => List[Char] ) = {
myCustomConcat("General", "Public", "License")
}
}
myCostumConcat 函数接受输入一个字符串数组,并返回一个包含每个字符串的第一个字母的列表。
所以,代码
myCustomConcat("General", "Public", "License") foreach print
将在控制台上打印:GPL
现在假设我想编写一个函数来生成 GPL 缩写词,使用(作为输入参数)我之前的函数提取每个字符串的第一个字母:
def GeneralPublicLicenceAcronym (concatFunction: (String*) => List[Char] ): List[Char] = {
myCustomConcat("General", "Public", "License")
}
通过部分应用程序运行这个新函数:
GeneralPublicLicenceAcronym(myCustomConcat(_)) foreach print
我收到此错误:
错误:(8, 46) 类型不匹配;发现:Seq[String] 必需:String GeneralPublicLicenceAcronym(myCustomConcat(_)) foreach print
为什么?在这种情况下我可以使用部分应用吗?最佳答案
您需要做的就是将 myCustomConcat(_)
更改为 myCustomConcat _
,或者实际上只是 myCustomConcat
您所做的并不完全是部分应用程序 - 它只是使用方法作为函数值。
在某些情况下(需要函数值),编译器会计算出您的意思,但在其他情况下,您通常需要使用 _
后缀告诉编译器您的意图。
“部分应用”意味着我们向函数提供部分(但不是全部)参数来创建新函数,例如:
def add(x: Int, y: Int) = x + y //> add: (x: Int, y: Int)Int
val addOne: Int => Int = add(1, _) //> addOne : Int => Int = <function1>
addOne(2) //> res0: Int = 3
我想你的情况可以被视为部分应用程序,但不应用任何参数 - 你可以在这里使用部分应用程序语法,但你需要给出一个_*
由于重复的参数(String*
)而向编译器提示,结果有点难看:
myCustomConcat(_:_*)
关于Scala函数部分应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30922251/