WelcomeTo
WelcomeTo

Reputation: 20591

Generate immutable collection in one expression

How to fill collection and then add one element to it without using mutable collection or declaring it as var?

In other words how I can use immutable collection in the following code instead of mutable.Buffer?

 val values: mutable.Buffer[MyClass] = {
    (for (i <- 1 until 10
    ) yield MyClass(Some(i)).toBuffer
  }

  values += MyClass(None)

Upvotes: 0

Views: 101

Answers (1)

om-nom-nom
om-nom-nom

Reputation: 62855

I switched to map, but with for-comprehension this should be the same:

val values = (1 until gridSize.size).map(i => MyClass(Some(i))) ++ Seq(MyClass(None), ...) 

Upvotes: 7

Related Questions