I see that it possible to uses the following syntax for method that take parameters of a repeated type:
def capitalizeAll( args: String*) = {
args.map { args => args.capitalize }
}
However I was wondering how an function can be used instead of "args => args.capitalize"
for example (does not work):
def func(s: String): String = { s.capitalize }
def capitalizeAll2( args: String*) = {
args.map { func( args ) }
}
how can I make this work? Cheers
There is no magic:
def func(s: String): String = { s.capitalize }
def capitalizeAll2( args: String*) = {
args.map { arg => func( arg ) }
}
Here I gave arg
name to currently processed string (out of all args
strings). Your first example works only because of shadowing (all strings are args
and current string given the same name, which just shadows original).
Almost no magic...
def capitalizeAll3( args: String*) = {
args.map(func)
}
The latest example uses syntax sugar to apply function with only one parameter to args.