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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.