How to convert immutable Seq to mutable seq with until loop

reyman64 picture reyman64 · May 8, 2011 · Viewed 7.6k times · Source

I'm trying to return a mutable Sequence with an until loop, but i have an immutable seq in return of (0 until nbGenomes) :

 def generateRandomGenome(nbGenomes:Int): IndexedSeq[GenomeDouble]={
    return ((0 until nbGenomes toSeq).map{e => generateRandomGenome}) 
  }

Return compilation error :

found   : scala.collection.immutable.IndexedSeq[org.openmole.tools.mgo.mappedgenome.genomedouble.GenomeDouble]
 required: scala.collection.mutable.IndexedSeq[org.openmole.tools.mgo.mappedgenome.genomedouble.GenomeDouble]
    return ((0 until nbGenomes toSeq).map{e => generateRandomGenome}) 

How i can force the until loop to return an mutable seq ? Thanks scala community!

Answer

Kim Stebel picture Kim Stebel · May 8, 2011

You can convert an immutable sequence to a mutable one by creating a new mutable sequence with the varargs constructor.

scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
scala> scala.collection.mutable.ArraySeq(l:_*)
res0: scala.collection.mutable.ArraySeq[Int] = ArraySeq(1, 2, 3)