# Scala algorithm: Rotate a matrix by 90 degrees clockwise

Published

## Algorithm goal

Rotate a matrix by 90 degrees, ie what was the first row going left-to-right becomes the last column top-to-bottom. For example,

 1 2 3 4

Becomes:

 3 1 4 2

## Test cases in Scala

``````assert(rotateMatrix(Map.empty) == Map.empty)
assert(
rotateMatrix(Map(MatrixIndex(1, 1) -> 1)) == Map(MatrixIndex(1, 1) -> 1)
)
assert(
rotateMatrix(
Map(
MatrixIndex(y = 1, x = 1) -> 1,
MatrixIndex(y = 1, x = 2) -> 2,
MatrixIndex(y = 2, x = 1) -> 3,
MatrixIndex(y = 2, x = 2) -> 4
)
) == Map(
MatrixIndex(y = 1, x = 1) -> 3,
MatrixIndex(y = 1, x = 2) -> 1,
MatrixIndex(y = 2, x = 1) -> 4,
MatrixIndex(y = 2, x = 2) -> 2
)
)
assert(
rotateMatrix(
Map(
MatrixIndex(y = 1, x = 1) -> 1,
MatrixIndex(y = 1, x = 2) -> 2,
MatrixIndex(y = 1, x = 3) -> 3,
MatrixIndex(y = 2, x = 1) -> 4,
MatrixIndex(y = 2, x = 2) -> 5,
MatrixIndex(y = 2, x = 3) -> 6
)
) == Map(
MatrixIndex(y = 1, x = 1) -> 4,
MatrixIndex(y = 1, x = 2) -> 1,
MatrixIndex(y = 2, x = 1) -> 5,
MatrixIndex(y = 2, x = 2) -> 2,
MatrixIndex(y = 3, x = 1) -> 6,
MatrixIndex(y = 3, x = 2) -> 3
)
)
``````

## Algorithm in Scala

18 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!

## Explanation

There are 2 key ways to represent a matrix: with a Map[Index, T], and with a Vector[Vector[T]] (similar to Array[Array[T]]). In the first case, the approach is to think about how to remap from one index to another, and in the second case, the approach is to think about how we can transform the data itself - for example, in the upcoming algorithm ReshapeMatrix, we use the Vector[Vector[T]] approach because the initial representation makes it easier to transform to the target one.

In the case of this problem, a rotation means that for each element, the new y is x, and the x depends on y (best advice is to see how each index maps to another index). (this is © from www.scala-algorithms.com)

### Scala specifics

The height is computed by simply finding the highest y-value. To compute it, we use a lazy val, as if we did a val, and the input were empty, there could be an empty list on which we cannot do a .max operation without a runtime exception. Height is only requested when there are elements, so we do not get a runtime exception when there are elements.

## Scala concepts & Hints

1. ### Pattern Matching

Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.

``````assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")
``````

# Scala Algorithms: The most comprehensive library of algorithms in standard pure-functional Scala

## How our 100 algorithms look

1. A description/goal of the algorithm.
2. An explanation with both Scala and logical parts.
3. A proof or a derivation, where appropriate.
4. Links to Scala concepts used in this specific algorithm, also unit-tested.
5. An implementation in pure-functional immutable Scala, with efficiency in mind (for most algorithms, this is for paid subscribers only).
6. Unit tests, with a button to run them immediately in our in-browser IDE.

### Study our 100 Scala Algorithms: 6 fully free, 100 published & 0 upcoming

Fully unit-tested, with explanations and relevant concepts; new algorithms published about once a week.

### Explore the 22 most useful Scala concepts

To save you going through various tutorials, we cherry-picked the most useful Scala concepts in a consistent form.

## Subscribe to Scala Algorithms

Maximize your Scala with disciplined and consistently unit-tested solutions to 100+ algorithms.

Use it from improving your day-to-day data structures and Scala; all the way to interviewing.