Reputation: 9
I have a function that takes 5 parameters:
MyFunction(arg1: String, arg2: String, ..., arg5: String)
And a List that has 4 Items
List("a","b","c","d")
I want to simply know how to pass the elements of the list as the 4 first parametres of the function in Scala
Upvotes: 0
Views: 69
Reputation: 51703
The easiest is to use pattern matching (as @LuisMiguelMejíaSuárez proposed in comments)
def myFunction(arg1: String, arg2: String, arg3: String, arg4: String, arg5: String): String =
arg1 + arg2 + arg3 + arg4 + arg5
val args = List("a","b","c","d")
val List(a, b, c, d) = args // <-- pattern matching
myFunction(a, b, c, d, "e") // abcde
More exotic way is
import shapeless.syntax.std.function._
import shapeless.syntax.std.traversable._
(myFunction _).toProduct((args :+ "e").toSizedHList(5).get) // abcde
Upvotes: 1