In JavaScript there is the nice spread operator.
Example from MDN:
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];
// ["head", "shoulders", "knees", "and", "toes"]
Is there an equivalent in Scala?
In JavaScript there is the nice spread operator.
Example from MDN:
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];
// ["head", "shoulders", "knees", "and", "toes"]
Is there an equivalent in Scala?
There's always patch()
. It's arguments are a little more cryptic because it has a wider, more general, field of applications.
val parts = List("shoulders", "knees")
val lyrics = List("head", "and", "toes")
lyrics.patch(1, parts, 0) // res0: List(head, shoulders, knees, and, toes)
This might work:
val parts = List("shoulders", "knees")
val lyrics = "head" :: parts.::("and").::("knees")
However, this works only on List
type
I think there's no equivalent.
You can do like this.
val parts = Seq("shoulders", "knees");
val lyrics = "head" +: parts :+ "and" :+ "toes"
println(lyrics) // List(head, shoulders, knees, and, toes)