FiFo (fi_fo v0.1.1) View Source

This module provides FIFO queues in an efficient manner.

FiFo is just a rewrite of the Erlang module queue in an Elixir way. The module includes implementations of the protocols Enumerabale, Collectable, and Inspect.

Link to this section Summary

Functions

Given a list of queues, concatenates the queues into a single queue. The first queue in the list becomes the front of the queue.

Concatenates the queue b with the queue a with queue a in front of queue b.

Drops the amount of elements from the queue.

Determines if the queue is empty.

Fetches element at the front of queue.

Fetches element at the front of queue, erroring out if queue is empty.

Fetches element at the rear of queue.

Fetches element at the rear of queue, erroring out if queue is empty.

Filters the queue, i.e. returns only those elements for which fun returns a truthy value.

Converts an Erlang queue to a queue.

Converts a list to a queue.

Converts a range to a queue.

Gets element at the front of queue, erroring out if queue is empty.

Gets element at the rear of queue, erroring out if queue is empty.

Returns a queue where each element is the result of invoking fun on each corresponding element of queue.

Checks if element exists within the queue.

Returns an empty queue.

Removes the element at the front of the queue. Returns tuple {{value, element}, queue}, where queue is the remaining queue. If the queue is empty a tuple {:error, %FiFo{}} is returned.

Removes the element at the front of the queue. Returns tuple {{value, element}, queue}, where queue is the remaining queue. If the queue is empty an EmptyError is raised.

Removes the element at the rear of the queue. Returns tuple {{value, element}, queue}, where queue is the remaining queue. If the queue is empty a tuple {:error, %FiFo{}} is returned.

Removes the element at the rear of the queue. Returns tuple {{value, element}, queue}, where queue is the remaining queue. If the queue is empty an EmptyError is raised.

Pushes an element to the rear of a queue.

Pushes an element to the front queue.

Returns a queue of elements in queue excluding those for which the function fun returns a truthy value.

Returns queue in reverse order.

Returns the number of elements in queue.

Takes an amount of elements from the rear or the front of the queue. Returns a tuple with taken values and the remaining queue.

Converts queue to an Erlang queue.

Converts queue to a list.

Link to this section Types

Specs

element() :: term()

Specs

empty() :: %FiFo{front: [], rear: []}

Specs

front() :: list()

Specs

rear() :: list()

Specs

t() :: %FiFo{front: list(), rear: list()}

Link to this section Functions

Specs

concat([t()]) :: t()

Given a list of queues, concatenates the queues into a single queue. The first queue in the list becomes the front of the queue.

Examples

iex> FiFo.concat([
...>   FiFo.from_range(1..3), FiFo.from_range(4..5), FiFo.from_range(7..9)
...> ])
#FiFo<[1, 2, 3, 4, 5, 7, 8, 9]>

Specs

concat(t(), t()) :: t()

Concatenates the queue b with the queue a with queue a in front of queue b.

Examples

iex> FiFo.concat(FiFo.from_list([1, 2]), FiFo.from_list([3, 4]))
#FiFo<[1, 2, 3, 4]>

Specs

drop(t(), non_neg_integer()) :: t()

Drops the amount of elements from the queue.

If a negative amount is given, the amount of last values will be dropped.

Examples

iex> [1, 2, 3] |> FiFo.from_list() |> FiFo.drop(2)
#FiFo<[3]>

iex> [1, 2, 3] |> FiFo.from_list() |> FiFo.drop(-2)
#FiFo<[1]>

iex> [1, 2, 3] |> FiFo.from_list() |> FiFo.drop(10)
#FiFo<[]>

iex> [1, 2, 3] |> FiFo.from_list() |> FiFo.drop(0)
#FiFo<[1, 2, 3]>

Specs

empty?(t()) :: boolean()

Determines if the queue is empty.

Returns true if queue is empty, otherwise false.

Examples

iex> FiFo.empty?(FiFo.new())
true

iex> FiFo.empty?(FiFo.from_list([1]))
false

Specs

fetch(t()) :: {:ok, element()} | :error

Fetches element at the front of queue.

If queue is not empty, then {:ok, element} is returned. If queue is empty :error is returned.

Examples

iex> FiFo.fetch(FiFo.from_list([1, 2]))
{:ok, 1}

iex> FiFo.fetch(FiFo.new())
:error

Specs

fetch!(t()) :: {:ok, element()}

Fetches element at the front of queue, erroring out if queue is empty.

If queue is not empty, then {:ok, element} is returned. If queue is empty a FiFo.EmptyError excepetion is raised.

Examples

iex> FiFo.fetch!(FiFo.from_list([1, 2]))
1

iex> FiFo.fetch!(FiFo.new())
** (FiFo.EmptyError) empty error

Specs

fetch_reverse(t()) :: {:ok, element()} | :error

Fetches element at the rear of queue.

If queue is not empty, then {:ok, element} is returned. If queue is empty :error is returned.

Examples

iex> FiFo.fetch_reverse(FiFo.from_list([1, 2]))
{:ok, 2}

iex> FiFo.fetch_reverse(FiFo.new())
:error

Specs

fetch_reverse!(t()) :: {:ok, element()} | :error

Fetches element at the rear of queue, erroring out if queue is empty.

If queue is not empty, then {:ok, element} is returned. If queue is empty a FiFo.EmptyError excepetion is raised.

Examples

iex> FiFo.fetch!(FiFo.from_list([1, 2]))
1

iex> FiFo.fetch!(FiFo.new())
** (FiFo.EmptyError) empty error

Specs

filter(t(), (element() -> as_boolean(element()))) :: t()

Filters the queue, i.e. returns only those elements for which fun returns a truthy value.

See also reject/2 which discards all elements where the function returns a truthy value.

Examples

iex> FiFo.filter(FiFo.from_list([1, 2, 3, 4]), fn x -> rem(x, 2) == 0 end)
#FiFo<[2, 4]>

Specs

from_erlang_queue({rear(), front()}) :: t()

Converts an Erlang queue to a queue.

Examples

iex> FiFo.from_erlang_queue({[3, 2], [1]})
#FiFo<[1, 2, 3]>

Specs

from_list(list()) :: t()

Converts a list to a queue.

Examples

iex> FiFo.from_list([1, 2, 3])
#FiFo<[1, 2, 3]>

Specs

from_range(Range.t()) :: t()

Converts a range to a queue.

Examples

iex> FiFo.from_range(1..3)
#FiFo<[1, 2, 3]>
Link to this function

get(queue, default \\ nil)

View Source

Specs

get(t(), term()) :: element() | term() | nil

Gets element at the front of queue, erroring out if queue is empty.

If queue is empty default is returned.

If default is not provided, nil is used.

Examples

iex> FiFo.get(FiFo.from_list([1, 2]))
1

iex> FiFo.get(FiFo.new())
nil

iex> FiFo.get(FiFo.new(), :empty)
:empty
Link to this function

get_reverse(queue, default \\ nil)

View Source

Specs

get_reverse(t(), term()) :: element() | term() | nil

Gets element at the rear of queue, erroring out if queue is empty.

If queue is empty default is returned.

If default is not provided, nil is used.

Examples

iex> FiFo.get_reverse(FiFo.from_list([1, 2]))
2

iex> FiFo.get_reverse(FiFo.new())
nil

iex> FiFo.get_reverse(FiFo.new(), :empty)
:empty

Specs

map(t(), (element() -> element())) :: t()

Returns a queue where each element is the result of invoking fun on each corresponding element of queue.

Examples

iex> FiFo.map(FiFo.from_list([1, 2, 3]), fn x -> x + 2 end)
#FiFo<[3, 4, 5]>

Specs

member?(t(), element()) :: boolean()

Checks if element exists within the queue.

Examples

iex> FiFo.member?(FiFo.from_list([1, 2, 3]), 2)
true
iex> FiFo.member?(FiFo.from_list([1, 2, 3]), 6)
false

Specs

new() :: t()

Returns an empty queue.

Examples

iex> FiFo.new()
#FiFo<[]>

Specs

pop(t()) :: {{:ok, element()}, t()} | {:error, empty()}

Removes the element at the front of the queue. Returns tuple {{value, element}, queue}, where queue is the remaining queue. If the queue is empty a tuple {:error, %FiFo{}} is returned.

Examples

iex> queue = FiFo.from_list([1,2,3])
iex> FiFo.pop(queue) == {{:ok, 1}, FiFo.drop(queue, 1)}
true

iex> FiFo.new() |> FiFo.pop() == {:error, %FiFo{}}
true

Specs

pop!(t()) :: {element(), t()}

Removes the element at the front of the queue. Returns tuple {{value, element}, queue}, where queue is the remaining queue. If the queue is empty an EmptyError is raised.

Examples

iex> queue = FiFo.from_list([1,2,3])
iex> FiFo.pop!(queue) == {1, FiFo.drop(queue, 1)}
true

iex> FiFo.pop!(FiFo.new()) == {:error, %FiFo{}}
** (FiFo.EmptyError) empty error

Specs

pop_reverse(t()) :: {{:ok, element()}, t()} | {:error, empty()}

Removes the element at the rear of the queue. Returns tuple {{value, element}, queue}, where queue is the remaining queue. If the queue is empty a tuple {:error, %FiFo{}} is returned.

Examples

iex> queue = FiFo.from_list([1,2,3])
iex> FiFo.pop_reverse(queue) == {{:ok, 3}, FiFo.drop(queue, -1)}
true

iex> FiFo.new() |> FiFo.pop_reverse() == {:error, %FiFo{}}
true

Specs

pop_reverse!(t()) :: {element(), t()}

Removes the element at the rear of the queue. Returns tuple {{value, element}, queue}, where queue is the remaining queue. If the queue is empty an EmptyError is raised.

Examples

iex> queue = FiFo.from_list([1,2,3])
iex> FiFo.pop_reverse!(queue) == {3, FiFo.drop(queue, -1)}
true

iex> FiFo.pop!(FiFo.new()) == {:error, %FiFo{}}
** (FiFo.EmptyError) empty error

Specs

push(t(), element()) :: t()

Pushes an element to the rear of a queue.

Examples

iex> queue = FiFo.new()
iex> queue = FiFo.push(queue, 2)
#FiFo<[2]>
iex> FiFo.push(queue, 4)
#FiFo<[2, 4]>

Specs

push_reverse(t(), element()) :: t()

Pushes an element to the front queue.

Examples

iex> queue = FiFo.new()
iex> queue = FiFo.push_reverse(queue, 2)
#FiFo<[2]>
iex> FiFo.push_reverse(queue, 4)
#FiFo<[4, 2]>

Specs

reject(t(), (element() -> as_boolean(element()))) :: t()

Returns a queue of elements in queue excluding those for which the function fun returns a truthy value.

See also filter/2.

Examples

iex> FiFo.reject(FiFo.from_list([1, 2, 3, 4]), fn x -> rem(x, 2) == 0 end)
#FiFo<[1, 3]>

Specs

reverse(t()) :: t()

Returns queue in reverse order.

Examples

iex> FiFo.reverse(FiFo.from_list([1, 2, 3]))
#FiFo<[3, 2, 1]>

Specs

size(t()) :: integer()

Returns the number of elements in queue.

Examples

iex> FiFo.size(FiFo.from_range(1..42))
42

Specs

take(t(), integer()) :: {list(), t()}

Takes an amount of elements from the rear or the front of the queue. Returns a tuple with taken values and the remaining queue.

If a negative amount is given, the amount of elements will be taken from rear.

Examples

iex> queue = FiFo.from_range(1..10)
iex> FiFo.take(queue, 3) == {[1, 2, 3], FiFo.drop(queue, 3)}
true
iex> FiFo.take(queue, 0) == {[], queue}
true

iex> FiFo.take(FiFo.new(), 10) == {[], FiFo.new()}
true

Specs

to_erlang_queue(t()) :: {rear(), front()}

Converts queue to an Erlang queue.

Examples

iex> q = FiFo.to_erlang_queue(FiFo.from_list([1, 2, 3, 4, 5]))
{[5, 4], [1, 2, 3]}
iex> q == :queue.from_list([1, 2, 3, 4, 5])
true

Specs

to_list(t()) :: list()

Converts queue to a list.

Examples

iex> FiFo.to_list(FiFo.from_range(1..4))
[1, 2, 3, 4]