e_q v1.0.0 EQ

A simple wrapper around the Erlang Queue library that follows the idiomatic pattern of expecting the module target first to take advantage of the pipeline operator.

Queues are double ended. The mental picture of a queue is a line of people (items) waiting for their turn. The queue front is the end with the item that has waited the longest. The queue rear is the end an item enters when it starts to wait. If instead using the mental picture of a list, the front is called head and the rear is called tail.

Entering at the front and exiting at the rear are reverse operations on the queue.

Summary

Functions

Returns true if the given queue is empty, false otherwise

With a given queue and function, a new queue is returned in the same order as the one given where the function returns true for an element

Returns a queue containing the items in L in the same order; the head item of the list will become the front item of the queue

Returns the item at the front of the queue. Returns the tuple {:value, item}``. If Q1 is empty, the tuple{:empty, Q1}` is returned

Returns true if the given item is a queue, false otherwise

Given two queues, an new queue is returned with the second appended to the end of the first queue given

Returns the item at the end of the queue. Returns the tuple {:value, item}. If Q1 is empty, the tuple {:empty, Q1} is returned

Calculates and returns the length of given queue

Returns true if the given element is in the queue, false otherwise

Returns an empty queue

Removes the item at the front of queue. Returns the tuple {{:value, item}, Q2}, where item is the item removed and Q2 is the resulting queue. If Q1 is empty, the tuple {:empty, Q1} is returned

Adds an item to the end of the queue, returns the resulting queue

Returns a new queue with the items for the given queue in reverse order

With a given queue and an amount it returns {Q2, Q3}, where Q2 contains the amount given and Q2 holds the rest. If attempted to split an empty queue or past the length an argument error is raised

Removes the item at the front of the queue, returns the resulting queue

Returns a list of the items in the queue in the same order; the front item of the queue will become the head of the list

Types

t :: %EQ{data: {[any], [any]}}

Functions

empty?(eq)

Specs

empty?(EQ.t) :: true | false

Returns true if the given queue is empty, false otherwise

Examples

iex> EQ.from_list([1, 2, 3]) |> EQ.empty?
false

iex> EQ.new |> EQ.empty?
true
filter(eq, fun)

Specs

filter(EQ.t, Fun) :: EQ.t

With a given queue and function, a new queue is returned in the same order as the one given where the function returns true for an element

Example

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

Specs

from_list([any]) :: EQ.t

Returns a queue containing the items in L in the same order; the head item of the list will become the front item of the queue.

Example

iex> EQ.from_list [1, 2, 3, 4, 5]
#EQ<[1, 2, 3, 4, 5]>
head(q)

Specs

head(EQ.t) :: {:value, any} | {:empty, EQ.t}

Returns the item at the front of the queue. Returns the tuple {:value, item}``. If Q1 is empty, the tuple{:empty, Q1}` is returned. ## Examples iex> EQ.from_list([1, 2]) |> EQ.head {:value, 1} iex> EQ.from_list([1]) |> EQ.head {:value, 1} iex> EQ.new |> EQ.head {:empty, EQ.new}

is_queue?(arg1)

Specs

is_queue?(any) :: true | false

Returns true if the given item is a queue, false otherwise

Examples

iex> EQ.new |> EQ.is_queue?
true

iex> {:a_queue?, [], []} |> EQ.is_queue?
false
join(eq1, eq2)

Specs

join(EQ.t, EQ.t) :: EQ.t

Given two queues, an new queue is returned with the second appended to the end of the first queue given

Example

iex> EQ.from_list([1]) |> EQ.join(EQ.from_list([2]))
#EQ<[1, 2]>
last(q)

Specs

last(EQ.t) :: {:value, any} | {:empty, EQ.t}

Returns the item at the end of the queue. Returns the tuple {:value, item}. If Q1 is empty, the tuple {:empty, Q1} is returned.

Examples

iex> EQ.from_list([1, 2]) |> EQ.last
{:value, 2}

iex> EQ.from_list([1]) |> EQ.last
{:value, 1}

iex> EQ.new |> EQ.last
{:empty, EQ.new}
length(eq)

Specs

length(EQ.t) :: pos_integer

Calculates and returns the length of given queue

Example

iex> EQ.from_list([:a, :b, :c]) |> EQ.length
3
member?(eq, item)

Specs

member?(EQ.t, any) :: true | false

Returns true if the given element is in the queue, false otherwise

Examples

iex> EQ.from_list([1, 2, 3]) |> EQ.member?(2)
true

iex> EQ.from_list([1, 2, 3]) |> EQ.member?(9)
false
new()

Specs

new :: EQ.t

Returns an empty queue

Example

iex> EQ.new
#EQ<[]>
pop(eq)

Specs

pop(EQ.t) :: {{:value, any}, EQ.t} | {:empty, EQ.t}

Removes the item at the front of queue. Returns the tuple {{:value, item}, Q2}, where item is the item removed and Q2 is the resulting queue. If Q1 is empty, the tuple {:empty, Q1} is returned.

Examples

iex> EQ.from_list([:a, :b]) |> EQ.pop
{{:value, :a}, %EQ{data: {[], [:b]} }}

iex> EQ.new |> EQ.pop
{:empty, EQ.new}
push(eq, item)

Specs

push(EQ.t, any) :: EQ.t

Adds an item to the end of the queue, returns the resulting queue

Example

iex> EQ.new |> EQ.push(:a)
#EQ<[:a]>
reverse(eq)

Specs

reverse(EQ.t) :: EQ.t

Returns a new queue with the items for the given queue in reverse order

Example

iex> EQ.from_list([1, 2, 3, 4, 5]) |> EQ.reverse
#EQ<[5, 4, 3, 2, 1]>
split(eq, amount)

Specs

split(EQ.t, pos_integer) :: {EQ.t, EQ.t}

With a given queue and an amount it returns {Q2, Q3}, where Q2 contains the amount given and Q2 holds the rest. If attempted to split an empty queue or past the length an argument error is raised

Examples

iex> EQ.from_list([1, 2, 3, 4, 5]) |> EQ.split(3)
{EQ.from_list([1,2,3]), EQ.from_list([4,5])}

iex> EQ.from_list([1, 2, 3, 4, 5]) |> EQ.split(12)
** (ArgumentError) argument error
tail(q)

Specs

tail(EQ.t) :: EQ.t

Removes the item at the front of the queue, returns the resulting queue.

Example

iex> EQ.from_list([1,2,3]) |> EQ.tail
#EQ<[2, 3]>

iex> EQ.from_list([1,2]) |> EQ.tail
#EQ<[2]>

iex> EQ.from_list([1]) |> EQ.tail
#EQ<[]>

iex> EQ.new |> EQ.tail
#EQ<[]>
to_list(eq)

Specs

to_list(EQ.t) :: [any]

Returns a list of the items in the queue in the same order; the front item of the queue will become the head of the list.

Example

iex> EQ.from_list([1, 2, 3, 4, 5]) |> EQ.to_list
[1, 2, 3, 4, 5]