The following generates a Vector of immutable Vectors:
var darr = Vector.tabulate(2, 3){ (a,b) => a*2+b }
darr: scala.collection.immutable.Vector[scala.collection.immutable.Vector[Int]] = Vector(Vector(0, 1, 2), Vector(2, 3, 4))
But what is needed for our use case is a Vector of mutable Vectors. How can that be done?
Just do nested calls to tabulate
, where the inner call is on a mutable sequence, like Buffer
:
import collection.mutable.Buffer
Vector.tabulate(2)(a => Buffer.tabulate(3)(b => a*2+b))
// Vector(ArrayBuffer(0, 1, 2), ArrayBuffer(2, 3, 4))
The 2D tabulate
is really just syntactic sugar for a nested tabulate
anyway:
// From GenTraversableFactory
def tabulate[A](n1: Int, n2: Int)(f: (Int, Int) => A): CC[CC[A]] =
tabulate(n1)(i1 => tabulate(n2)(f(i1, _)))