2

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?

1

4 Answers 4

4

How about

val lyrics = Seq("head") ++ parts ++ Seq("and", "toes")
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

0

This might work:

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

However, this works only on List type

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.