我想了解功能部分应用如何在斯卡拉工作。斯卡拉功能部分应用
为了做到这一点,我已经建立了这个简单的代码:
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功能在输入采用一个String数组,它返回一个包含每个字符串的第一个字母列表。
所以,代码
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] required:String GeneralPublicLicenceAcronym(myCustomConcat(_))foreach print
为什么?我可以在这种情况下使用部分申请吗?