I'm trying to understand how function partial application works in Scala.
To do that, I've built this simple code:
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 function takes in input an array of String and it returns a list containing the first letter of each string.
So, the code
myCustomConcat("General", "Public", "License") foreach print
will print on console: GPL
Suppose now that I want to write a function to generate the GPL acronym, using (as input parameter) my previous function extracting the first letter of each string:
def GeneralPublicLicenceAcronym (concatFunction: (String*) => List[Char] ): List[Char] = {
myCustomConcat("General", "Public", "License")
}
Running this new function with partial application:
GeneralPublicLicenceAcronym(myCustomConcat(_)) foreach print
I get this error:
Error:(8, 46) type mismatch; found : Seq[String] required: String GeneralPublicLicenceAcronym(myCustomConcat(_)) foreach print
Why? Can I use partial application in this case?