Scala algorithm: Reshape a matrix
Turn an m-by-n matrix into an n-by-m matrix, retaining the order of the elements. For example:
Test cases in Scala
Algorithm in Scala
4 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!
In Scala this is straightforward: flatten the whole matrix, and then group the elements into chunks of length of the height of the matrix (.grouped). (this is © from www.scala-algorithms.com)
Scala concepts & Hints
.viewsyntax creates a structure that mirrors another structure, until "forced" by an eager operation like .toList, .foreach, .forall, .count.