yaq v1.0.0 Yaq View Source

Yet Another Queue module for Elixir

Link to this section Summary

Functions

Remove an item from the front of the queue.

Remove an item from the rear of the queue.

Push a new term onto the rear of the queue.

Push a new term onto the front of the queue.

Create a new queue.

Return the front element of the queue. Returns nil if empty

Returns the rear element of the queue.

Return the number of elements in the queue.

Return the elements of the queue as a list.

Link to this section Types

Specs

t()

Specs

value() :: nil | term()

Link to this section Functions

Specs

dequeue(t()) :: {value(), t()}

Remove an item from the front of the queue.

Examples

iex> {term, q} = Yaq.new(1..3) |> Yaq.dequeue()
iex> term
1
iex> q
#Yaq<length: 2>

Specs

dequeue_r(t()) :: {value(), t()}

Remove an item from the rear of the queue.

Examples

iex> {term, q} = Yaq.new(1..3) |> Yaq.dequeue_r()
iex> term
3
iex> q
#Yaq<length: 2>

Specs

enqueue(t(), term()) :: t()

Push a new term onto the rear of the queue.

Parameters

  • q: Current queue

  • term: Elixir term to enqueue

Examples

iex> q = Yaq.new()
#Yaq<length: 0>
iex> q = Yaq.enqueue(q, 1)
#Yaq<length: 1>
iex> q = Yaq.enqueue(q, 2)
#Yaq<length: 2>
iex> Yaq.to_list(q)
[1, 2]

Specs

enqueue_r(t(), term()) :: t()

Push a new term onto the front of the queue.

Examples

iex> q = Yaq.new()
#Yaq<length: 0>
iex> q = Yaq.enqueue_r(q, 1)
#Yaq<length: 1>
iex> q = Yaq.enqueue_r(q, 2)
#Yaq<length: 2>
iex> Yaq.to_list(q)
[2, 1]

Specs

new(Enumerable.t()) :: t()

Create a new queue.

Parameters

  • enum (optional): initial queue data

Examples

iex> Yaq.new()
#Yaq<length: 0>

iex> Yaq.new(1..10)
#Yaq<length: 10>

Specs

peek(t()) :: value()

Return the front element of the queue. Returns nil if empty

Parameters

  • q: Current queue

Examples

iex> Yaq.new() |> Yaq.peek()
nil

iex> Yaq.new(1..3) |> Yaq.peek()
1

Specs

peek_r(t()) :: value()

Returns the rear element of the queue.

Parameters

  • q: Current queue

Examples

iex> Yaq.new() |> Yaq.peek_r()
nil

iex> Yaq.new(1..3) |> Yaq.peek_r()
3

Specs

size(t()) :: non_neg_integer()

Return the number of elements in the queue.

Parameters

  • q: Current queue

Examples

iex> Yaq.new() |> Yaq.size()
0

iex> Yaq.new(1..3) |> Yaq.size()
3

Specs

to_list(t()) :: list()

Return the elements of the queue as a list.

Parameters

  • q: Current queue

Examples

iex> Yaq.new([1, 2, 3]) |> Yaq.to_list()
[1, 2, 3]