Search code examples
scalaimmutabilitymutable

How to convert an inmutable Seq of immutable Seq into a mutable Seq of mutable Seq in Scala?


Let an immutable Seq of immutable Seqs be:

val content: Seq[Seq[Double]

that I'd like to transform as a mutable Seq of mutable Seqs:

val mutable_being_inversed_matrix:
  collection.mutable.Seq[collection.mutable.Seq[Double]] =
    content.to[collection.mutable.Seq[collection.mutable.Seq[Double]]

But this generates the following error:

Error:(79, 128)
  scala.collection.mutable.Seq[scala.collection.mutable.Seq[Double]]
    takes no type parameters, expected: one
  val mutable_being_inversed_matrix: collection.mutable.Seq[collection.mutable.Seq[Double]] = content.to[collection.mutable.Seq[collection.mutable.Seq[Double]]]

How to deal with it?


Solution

  • Given this immutable input:

    val immutableInput = Seq(Seq(1, 2), Seq(4))
    

    you can switch to a mutable Seq of mutable Seqs by using the varargs constructor:

    scala.collection.mutable.Seq(
      immutableInput.map(imseq => scala.collection.mutable.Seq(imseq:_*)):_*
    )
    

    which produces:

    res0: scala.collection.mutable.Seq[scala.collection.mutable.Seq[Int]] =
      ArrayBuffer(ArrayBuffer(1, 2), ArrayBuffer(4))