View Source FiFo (FiFo v0.2.0)

This module provides FIFO queues in an efficient manner.

FiFo is just a rewrite of the Erlang module queue in an Elixir way.

All queues generated with functions from this module are wellformed. A malformed queue is a queue with a front-list with more as one value and an empty list for the rear-list or the other way around. This module handles also malformed queues, just slower.

Link to this section Summary

Functions

Returns true if fun.(value) is truthy for all values in the queue.

Returns true if fun.(value) is truthy for at least one value in the queue.

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 right with the queue left with queue left in front of queue right.

Drops the amount of elements from the queue.

Determines if the queue is empty.

Fetches value at the front of queue.

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

Fetches value at the rear of queue.

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

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

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

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

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

Checks if value exists within the queue.

Returns an empty queue.

Return a queue from the given enumerable.

Returns the first item from the queue.

Returns the last item from the queue.

Pushes a list of values to a queue.

Pushes an element to the front queue.

Puts the given value to the rear of the queue.

Puts the given value to the front of the 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 a list.

Link to this section Types

@type empty() :: {[], []}
@type front() :: list()
@type queue() :: {rear(), front()}
@type rear() :: list()
@type value() :: any()

Link to this section Functions

@spec all?(queue(), (value() -> as_boolean(value()))) :: boolean()

Returns true if fun.(value) is truthy for all values in the queue.

@spec any?(queue(), (value() -> as_boolean(value()))) :: boolean()

Returns true if fun.(value) is truthy for at least one value in the queue.

@spec concat([queue()]) :: queue()

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

Examples

iex> FiFo.concat([
...>   FiFo.new(1..3), FiFo.new(4..5), FiFo.new(7..9)
...> ])
{[9, 8, 7, 5], [1, 2, 3, 4]}
@spec concat(left :: queue(), right :: queue()) :: queue()

Concatenates the queue right with the queue left with queue left in front of queue right.

examples

Examples

iex> FiFo.concat(FiFo.new([1, 2]), FiFo.new([3, 4]))
{[4, 3], [1, 2]}
@spec drop(queue(), non_neg_integer()) :: queue()

Drops the amount of elements from the queue.

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

examples

Examples

iex> [1, 2, 3] |> FiFo.new() |> FiFo.drop(2)
{[3],[]}

iex> [1, 2, 3] |> FiFo.new() |> FiFo.drop(-2)
{[],[1]}

iex> [1, 2, 3] |> FiFo.new() |> FiFo.drop(10)
{[], []}

iex> [1, 2, 3] |> FiFo.new() |> FiFo.drop(0)
{[3, 2], [1]}
@spec empty?(queue()) :: boolean()

Determines if the queue is empty.

Returns true if queue is empty, otherwise false.

examples

Examples

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

iex> FiFo.empty?(FiFo.new([1]))
false
@spec fetch(queue()) :: {{:ok, value()}, queue()} | {:error, empty()}

Fetches value at the front of queue.

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

examples

Examples

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

iex> FiFo.fetch(FiFo.new())
{:error, {[], []}}
@spec fetch!(queue()) :: value()

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

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

examples

Examples

iex> FiFo.fetch!(FiFo.new([1, 2]))
{1, {[2], []}}

iex> FiFo.fetch!(FiFo.new())
** (FiFo.EmptyError) empty queue
@spec fetch_reverse(queue()) :: {{:ok, value()}, queue()} | {:error, empty()}

Fetches value at the rear of queue.

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

examples

Examples

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

iex> FiFo.fetch_reverse(FiFo.new())
{:error, {[], []}}
@spec fetch_reverse!(queue()) :: {:ok, value()} | :error

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

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

examples

Examples

iex> q = FiFo.new()
iex> FiFo.fetch!(q)
** (FiFo.EmptyError) empty queue

iex> q = FiFo.new([1, 2, 3])
iex> FiFo.fetch!(q)
{1, {[3], [2]}}
@spec filter(queue(), fun :: (value() -> as_boolean(value()))) :: queue()

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

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

examples

Examples

iex> [1, 2, 3, 4]
...> |> FiFo.new()
...> |> FiFo.filter(fn x -> rem(x, 2) == 0 end)
...> |> FiFo.to_list()
[2, 4]
Link to this function

get(queue, default \\ nil)

View Source
@spec get(queue(), default :: value()) :: value()

Gets value 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

Examples

iex> FiFo.get(FiFo.new([1, 2]))
{1, {[2], []}}

iex> FiFo.get(FiFo.new())
{nil, {[], []}}

iex> FiFo.get(FiFo.new(), :empty)
{:empty, {[], []}}
Link to this function

get_reverse(queue, default \\ nil)

View Source
@spec get_reverse(queue(), default :: value()) :: value()

Gets value 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

Examples

iex> FiFo.get_reverse(FiFo.new([1, 2]))
{2, {[1], []}}

iex> FiFo.get_reverse(FiFo.new())
{nil, {[], []}}

iex> FiFo.get_reverse(FiFo.new(), :empty)
{:empty, {[], []}}
@spec map(queue(), (value() -> value())) :: queue()

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

examples

Examples

iex> FiFo.map(FiFo.new([1, 2, 3]), fn x -> x + 2 end)
{[5, 4], [3]}
@spec member?(queue(), value()) :: boolean()

Checks if value exists within the queue.

examples

Examples

iex> FiFo.member?(FiFo.new([1, 2, 3]), 2)
true
iex> FiFo.member?(FiFo.new([1, 2, 3]), 6)
false
@spec new() :: queue()

Returns an empty queue.

examples

Examples

iex> FiFo.new()
{[], []}
@spec new(enumerable :: Enumerable.t()) :: queue()

Return a queue from the given enumerable.

examples

Examples

iex> FiFo.new([1, 2, 3])
{[3, 2], [1]}

iex> FiFo.new(1..10)
{[10, 9, 8, 7, 6], [1, 2, 3, 4, 5]}
Link to this function

peek(queue, default \\ nil)

View Source
@spec peek(queue(), default :: value()) :: queue()

Returns the first item from the queue.

Link to this function

peek_reverse(queue, default \\ nil)

View Source
@spec peek_reverse(queue(), default :: value()) :: queue()

Returns the last item from the queue.

@spec push(queue(), [value()]) :: queue()

Pushes a list of values to a queue.

examples

Examples

iex> queue = FiFo.new()
iex> queue = FiFo.push(queue, [1, 2])
{[2], [1]}
iex> FiFo.push(queue, [3, 4])
{[4, 3, 2], [1]}
Link to this function

push_reverse(queue, list)

View Source
@spec push_reverse(queue(), [value()]) :: queue()

Pushes an element to the front queue.

examples

Examples

iex> queue = FiFo.new()
iex> queue = FiFo.push_reverse(queue, [3, 4])
{[4], [3]}
iex> FiFo.push_reverse(queue, [1, 2])
{[4], [1, 2, 3]}
@spec put(queue(), value()) :: queue()

Puts the given value to the rear of the queue.

Link to this function

put_reverse(queue, value)

View Source
@spec put_reverse(queue(), value()) :: queue()

Puts the given value to the front of the queue.

@spec reject(queue(), (value() -> as_boolean(value()))) :: queue()

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

See also filter/2.

examples

Examples

iex> FiFo.reject(FiFo.new([1, 2, 3, 4]), fn x -> rem(x, 2) == 0 end)
{[3], [1]}
@spec reverse(queue()) :: queue()

Returns queue in reverse order.

examples

Examples

iex> FiFo.reverse(FiFo.new([1, 2, 3]))
{[1], [3,2]}
@spec size(queue()) :: integer()

Returns the number of elements in queue.

examples

Examples

iex> FiFo.size(FiFo.new(1..42))
42
@spec take(queue(), amount :: integer()) :: {[value()], 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.

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

examples

Examples

iex> queue = FiFo.new(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
@spec to_list(queue()) :: list()

Converts queue to a list.

examples

Examples

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