yaq v1.2.0 Yaq View Source

Yet Another Queue module for Elixir

Link to this section Summary

Functions

Concatenate an enumerable to the rear of the queue.

Concatenate an enumerable to the front of the queue.

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.

Fetches the front value from the queue.

Fetches the front value from the queue.

Fetches the rear value from the queue.

Fetches the rear value from the queue.

Create a new queue.

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

Returns the rear element of the queue.

Reverse the queue.

Return the number of elements in the queue.

Splits the queue in two. Takes up to the first n elements from the front of the queue as the first queue and returns the remainder as the second.

Return the elements of the queue as a list.

Link to this section Types

Specs

t()

Specs

t(value)

Specs

value() :: term()

Link to this section Functions

Specs

concat(t(), Enumerable.t()) :: t()

Concatenate an enumerable to the rear of the queue.

Parameters

  • q: Current quque
  • enum: Items to add

Examples

iex> q = Yaq.new(1..10) |> Yaq.concat(11..20)
#Yaq<length: 20>
iex> Yaq.to_list(q)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Specs

concat_r(t(), Enumerable.t()) :: t()

Concatenate an enumerable to the front of the queue.

Parameters

  • q: Current quque
  • enum: Items to add

Examples

iex> q = Yaq.new(1..10) |> Yaq.concat_r(11..20)
#Yaq<length: 20>
iex> Yaq.to_list(q)
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Link to this function

dequeue(q, default \\ nil)

View Source

Remove an item from the front of the queue.

Parameters

  • q: The current queue
  • default (optional): Return value if the queue is empty. Defaults to nil

Examples

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

iex> {term, q} = Yaq.new() |> Yaq.dequeue()
iex> term
nil
iex> q
#Yaq<length: 0>

iex> {term, q} = Yaq.new() |> Yaq.dequeue(:user_defined)
iex> term
:user_defined
iex> q
#Yaq<length: 0>
Link to this function

dequeue_r(q, default \\ nil)

View Source

Remove an item from the rear of the queue.

Parameters

  • q: The current queue
  • default (optional): Return value if the queue is empty. Defaults to nil

## Examples

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

iex> {term, q} = Yaq.new() |> Yaq.dequeue_r()
iex> term
nil
iex> q
#Yaq<length: 0>

iex> {term, q} = Yaq.new() |> Yaq.dequeue_r(:user_defined)
iex> term
:user_defined
iex> q
#Yaq<length: 0>

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

fetch(t()) :: {value(), t()} | :error

Fetches the front value from the queue.

If the queue is empty, reutrns the :error. Otherwise, returns the tuple {value, updated_q}.

Parameters

  • q: Current queue

Examples

iex> Yaq.new() |> Yaq.fetch()
:error

iex> {response, queue} = Yaq.new([1, 2, 3]) |> Yaq.fetch()
iex> response
1
iex> queue
#Yaq<length: 2>

Specs

fetch!(t()) :: {value(), t()}

Fetches the front value from the queue.

If the queue is empty, raises Yaq.EmptyQueueError. Otherwise, returns the tuple {value, q}.

Parameters

  • q: Current queue

Examples

iex> Yaq.new() |> Yaq.fetch!()
** (Yaq.EmptyQueueError) empty queue

iex> {response, queue} = Yaq.new([1, 2, 3]) |> Yaq.fetch!()
iex> response
1
iex> queue
#Yaq<length: 2>

Specs

fetch_r(t()) :: {{:ok, value()}, t()} | :error

Fetches the rear value from the queue.

If the queue is empty, reutrns the :error. Otherwise, returns the tuple {{:ok, value}, q}.

Parameters

  • q: Current queue

Examples

iex> Yaq.new() |> Yaq.fetch_r()
:error

iex> {response, queue} = Yaq.new([1, 2, 3]) |> Yaq.fetch_r()
iex> response
3
iex> queue
#Yaq<length: 2>

Specs

fetch_r!(t()) :: {value(), t()}

Fetches the rear value from the queue.

If the queue is empty, raises Yaq.EmptyQueueError. Otherwise, returns the tuple {value, q}.

Parameters

  • q: Current queue

Examples

iex> Yaq.new() |> Yaq.fetch_r!()
** (Yaq.EmptyQueueError) empty queue

iex> {response, queue} = Yaq.new([1, 2, 3]) |> Yaq.fetch_r!()
iex> response
1
iex> queue
#Yaq<length: 2>

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

Reverse the queue.

Parameters

  • q: Current queue

Examples

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

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

split(t(), non_neg_integer()) :: {t(), t()}

Splits the queue in two. Takes up to the first n elements from the front of the queue as the first queue and returns the remainder as the second.

If n is larger than the size of q, then the first queue will be q and the second will be empty.

Parameters

  • q: Current queue
  • n: Max size of the first queue

Examples

iex> {left, right} = Yaq.new(1..10) |> Yaq.split(0)
iex> Yaq.to_list(left)
[]
iex> Yaq.to_list(right)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

iex> {left, right} = Yaq.new(1..10) |> Yaq.split(10)
iex> Yaq.to_list(left)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iex> Yaq.to_list(right)
[]

iex> {left, right} = Yaq.new(1..10) |> Yaq.split(20)
iex> Yaq.to_list(left)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iex> Yaq.to_list(right)
[]

iex> {left, right} = Yaq.new(1..10) |> Yaq.split(3)
iex> Yaq.to_list(left)
[1, 2, 3]
iex> Yaq.to_list(right)
[4, 5, 6, 7, 8, 9, 10]

iex> {left, right} = Yaq.new(1..10) |> Yaq.split(8)
iex> Yaq.to_list(left)
[1, 2, 3, 4, 5, 6, 7, 8]
iex> Yaq.to_list(right)
[9, 10]

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]