我想了解功能部分應用如何在斯卡拉工作。斯卡拉功能部分應用
爲了做到這一點,我已經建立了這個簡單的代碼:
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
爲什麼?我可以在這種情況下使用部分申請嗎?