# Scala algorithm: Rotate Array right in pure-functional Scala - using an unusual immutable efficient approach

Published

## Algorithm goal

Rotating the array $$[1,2,3]$$ by 1 element to the right returns an array $$[3,1,2]$$. Example:

 1 2 3
 3 1 2
 2 3 1

Rotating the array $$[1,2,3]$$ 3 times returns us to the original array $$[1,2,3]$$.

## Test cases in Scala

assert(countToDrop(length = 0, rightShift = 0) == 0)
assert(countToDrop(length = 1, rightShift = 0) == 1)
assert(countToDrop(length = 5, rightShift = 0) == 5)
assert(countToDrop(length = 5, rightShift = 1) == 4)
assert(countToDrop(length = 5, rightShift = 5) == 5)
assert(countToDrop(length = 4, rightShift = 2) == 2)
assert(rotateArrayRight(Array(), 3).isEmpty)
assert(rotateArrayRight(Array(1), 3).toVector == Vector(1))
assert(rotateArrayRight(Array(1, 2), 0).toVector == Vector(1, 2))
assert(
rotateArrayRight(Array(1, 2, 3, 4), 1).toVector == Vector(4, 1, 2, 3)
)
assert(
rotateArrayRight(Array(1, 2, 3, 4), 5).toVector == Vector(4, 1, 2, 3)
)


## Algorithm in Scala

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

## Explanation

The most tricky part is finding the trick - which is to join two Arrays side-by-side, and then slice that array to get a continuous slice that simulates a rotation - no mutations needed!

In Scala, we use the View concept to turn our array $$[1,2,3]$$ into an array $$[1,2,3,1,2,3]$$ with concatenation (without doing any allocations yet!), and then select the expected slice. (this is Â© from www.scala-algorithms.com)

For example, when $$n = 1$$:

 1 2 3 1 2 3

## Scala concepts & Hints

1. ### Drop, Take, dropRight, takeRight

Scala's drop and take methods typically remove or select n items from a collection.

assert(List(1, 2, 3).drop(2) == List(3))

assert(List(1, 2, 3).take(2) == List(1, 2))

assert(List(1, 2, 3).dropRight(2) == List(1))

assert(List(1, 2, 3).takeRight(2) == List(2, 3))

assert((1 to 5).take(2) == (1 to 2))

2. ### View

The .view syntax creates a structure that mirrors another structure, until "forced" by an eager operation like .toList, .foreach, .forall, .count.

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

### Study our 92 Scala Algorithms: 6 fully free, 87 published & 5 upcoming

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

### Explore the 21 most useful Scala concepts

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

## Register now (free)

Register with GitHub

## How the 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.