In Spark code, we usually put the case statement on the same as as the map (when there is only one case)
```scala
    val rows = sp.data.map(x => (x.i, (x.j, x.mval))).groupByKey.map { case (i, cols) =>
      val rowarray = Array.ofDim[Double](n)
      var j = 0
      while (j < cols.length) {
        rowarray(cols(j)._1) = cols(j)._2
        j += 1 
      }
      MatrixRow(i, rowarray)
    }
```

Make sure you use array.length instead of array.size because the former is actually faster (doesn't require an implicit conversion into ArrayOps)