1

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Piu130
  • 1,288
  • 3
  • 16
  • 28
  • Maybe this is of interest (more in the context of spreading over function parameters): https://stackoverflow.com/questions/15170646/spread-parameters-in-scala?rq=1 – Thilo Aug 26 '17 at 08:09

4 Answers4

4

How about

val lyrics = Seq("head") ++ parts ++ Seq("and", "toes")
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

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)
jwvh
  • 50,871
  • 7
  • 38
  • 64
0

This might work:

val parts = List("shoulders", "knees")
val lyrics = "head" :: parts.::("and").::("knees")

However, this works only on List type

Genry
  • 1,358
  • 2
  • 23
  • 39
0

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)
Aha00a
  • 1
  • 4