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
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.
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]}
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]}
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
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, {[], []}}
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
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, {[], []}}
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]
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, {[], []}}
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, {[], []}}
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]}
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]}
Returns the first item from the queue.
Returns the last item from the 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]}
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]}
Puts the given value to the rear of the 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]}
Returns queue in reverse order.
examples
Examples
iex> FiFo.reverse(FiFo.new([1, 2, 3]))
{[1], [3,2]}
Returns the number of elements in queue.
examples
Examples
iex> FiFo.size(FiFo.new(1..42))
42
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
Converts queue to a list.
examples
Examples
iex> FiFo.to_list(FiFo.new(1..4))
[1, 2, 3, 4]