# Scala algorithm: Make a queue using Maps

Published

## Algorithm goal

Make a pure-functional queue using Scala's `Map`. It would have the following methods: `isEmpty: Boolean`, `enqueue(T)`, dequeue: `Option[(T, Queue[T])]`.

Try to make it efficient, where possible.

## Test cases in Scala

``````assert(Queue.empty.isEmpty)
assert(Queue.empty.enqueue("A").dequeue.contains("A" -> Queue.empty))
assert(
Queue.empty.enqueue("A").enqueue("B").dequeue.map(_._1).contains("A")
)
assert(
Queue.empty
.enqueue("A")
.enqueue("B")
.dequeue
.map(_._2)
.flatMap(_.dequeue)
.map(_._1)
.contains("B")
)
assert(
Queue.empty.enqueue("A").enqueue("B").dequeue.map(_._2).forall(_.nonEmpty)
)
assert(
Queue.empty
.enqueue("A")
.enqueue("B")
.dequeue
.map(_._2)
.flatMap(_.dequeue)
.map(_._2)
.flatMap(_.dequeue)
.isEmpty
)
assert(
Queue.empty
.enqueue("A")
.enqueue("B")
.dequeue
.map(_._2.enqueue("C"))
.flatMap(_.dequeue)
.map(_._1)
.contains("B")
)
assert(
Queue.empty
.enqueue("A")
.enqueue("B")
.dequeue
.map(_._2.enqueue("C"))
.flatMap(_.dequeue)
.map(_._2)
.flatMap(_.dequeue)
.map(_._1)
.contains("C")
)
assert(
Queue.empty
.enqueue("A")
.enqueue("B")
.dequeue
.map(_._2.enqueue("C"))
.flatMap(_.dequeue)
.map(_._2)
.flatMap(_.dequeue)
.map(_._2)
.flatMap(_.dequeue)
.isEmpty
)
``````

## Algorithm in Scala

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

## Explanation

The basic idea here is that we can use a Map as a pointer to store items, where the key is the order of the item in the queue. Then, we need to know the range of keys that are in this Map, and the perfect data structure is Scala's range.

For the first item, the range is '0 to 0' ie only 1 item, after a second item the range is '0 to 1', and after dequeueing it, the range becomes '1 to 1'; if we tried to find the range from the Map we'd be facing an operation more expensive than O(1), and for that reason we should make the range explicit. (this is Â© from www.scala-algorithms.com)

## 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. ### Option Type

The 'Option' type is used to describe a computation that either has a result or does not. In Scala, you can 'chain' Option processing, combine with lists and other data structures. For example, you can also turn a pattern-match into a function that return an Option, and vice-versa!

``````assert(Option(1).flatMap(x => Option(x + 2)) == Option(3))

assert(Option(1).flatMap(x => None) == None)
``````
3. ### Range

The `(1 to n)` syntax produces a "Range" which is a representation of a sequence of numbers.

``````assert((1 to 5).toString == "Range 1 to 5")

assert((1 to 5).reverse.toString() == "Range 5 to 1 by -1")

assert((1 to 5).toList == List(1, 2, 3, 4, 5))
``````
4. ### State machine

A state machine is the use of `sealed trait` to represent all the possible states (and transitions) of a 'machine' in a hierarchical form.

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

### Study our 89 Scala Algorithms: 6 fully free, 89 published & 0 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.