I know how to define a method with variable length argument:
case class taxonomy(vocabularies:(String,Set[String])*)
and client code is very clean:
val terms=taxonomy("topics"->Set("economic","politic")
,"tag"->Set("Libya","evolution")
)
but I want to Know how can I use this case class when I have a variable (instead of a Sequence of variable) like this:
val notFormattedTerms = Map("topics"->Set("economic","politic")
,"tag"->Set("Libya","evolution"))
Practical Data Science using Python As the name implies, an argument with a variable length can take on a variety of values. You define a variable argument using a '*', for example *args, to show that the function can take a variable number of arguments.
A variable-length argument is a feature that allows a function to receive any number of arguments. There are situations where a function handles a variable number of arguments according to requirements, such as: Sum of given numbers. Minimum of given numbers and many more.
Variable-length arguments, abbreviated as varargs, are defined as arguments that can also accept an unlimited amount of data as input. The developer doesn't have to wrap the data in a list or any other sequence while using them. Let's understand the concept with the help of an example.
Scala - Function with Variable Arguments This allows clients to pass variable length argument lists to the function. Here, the type of args inside the print Strings function, which is declared as type "String*" is actually Array[String].
taxonomy(notFormattedTerms.toSeq:_*)
With : _*
you virtually transform a sequence argument so that it looks as if a several arguments had been passed to the variable length method. This transformation, however, only works for (ordered?) simple sequence types and, as in this case, not for a Map
. Therefore, one will have to use an explicit toSeq
before.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With