View Source QueueAssistant (Mishka developer tools v0.1.8)
This is a simple wrapper for the Erlang queue, in which the order of some entries has also been changed
- Based on: https://www.erlang.org/doc/man/queue
Summary
Functions
Returns true if all elements in enumerable are truthy. It is like Enum.all?/1
.
Returns true if at least one element in enumerable is truthy.
Inserts Item at the head of queue q1
. Returns the new queue q2
.
For see more information, check len/1
Returns the tail item of a queue.
Returns a copy of q1
where the first item matching item
is deleted, if there is such an item.
Returns a copy of q1
where the last item matching item
is deleted, if there is such an item.
Returns a copy of q1
where the first item for which fn
returns true is deleted,
if there is such an item.
Returns a copy of q1
where the last item for which fn
returns true is deleted,
if there is such an item.
Returns a queue q2
that is the result of removing the front item from q1
.
Returns a queue q2
that is the result of removing the rear item from q1
.
For see more information, check is_empty?/1
Filters the enumerable, i.e. returns only those elements for which fun returns a truthy value.
It is like Enum.filter/2
.
From Erlang doc:
Returns a queue q2
that is the result of calling Fun(Item)
on all items in q1
.
Please see head/1
.
Invokes fun for each element in the enumerable with the accumulator. It is like Enum.reduce/3
.
Returns a queue containing the items in the same order; the head item of the list becomes the front item of the queue.
Returns Item at the front of a queue.
Returns Item at the rear of a queue.
Returns the head item of a queue.
For see more information, check insert_r/2
Returns a queue q2
that is the result of removing the tail item from q1
.
Inserts Item at the rear of queue q1
. Returns the resulting queue q2
.
Inserts Item at the front of queue q1
. Returns the resulting queue q2
.
Tests if a queue is empty and returns true if so, otherwise false.
Tests if an entry is queue and returns true if so, otherwise false.
Returns a queue q3
that is the result of joining q1
and q2
with q1
in front of q2
.
Returns the tail item of a queue.
Calculates and returns the length of a queue.
It is like init/1
.
Checks if element exists within the queue.
Returns an empty queue.
Removes the item at the front of queue q1
. Returns tuple {{:value, item}, q2}
,
where Item is the item removed and q2
is the resulting queue.
If q1
is empty, tuple {empty, q1}
is returned.
Removes the item at the rear of queue q1
. Returns tuple {{:value, item}, q2}
,
where Item is the item removed and q2
is the resulting queue.
If q1
is empty, tuple {empty, q1}
is returned.
Returns tuple {:value, item}
, where Item is the front item of a queue,
or empty if a queue is :empty
.
Returns tuple {:value, item}
, where Item is the rear item of a queue,
or empty if a queue is :empty
.
For see more information, check is_queue?/1
For see more information, check fold/3
Returns a queue q2
containing the items of q1
in the reverse order.
Inserts Item as the tail item of a queue q1
. Returns a new queue like q2
.
Splits q1
in two. The n
front items are put in q2
and the rest in q3
.
Returns a queue q2
that is the result of removing the head item from q1
.
Returns a list of the items in the queue in the same order; the front item of the queue becomes the head of the list.
Types
@type queue_type() :: :queue.queue(any())
Functions
@spec all?((any() -> boolean()), queue_type()) :: boolean()
Returns true if all elements in enumerable are truthy. It is like Enum.all?/1
.
Example:
new()
|> insert(1)
|> all?(fn x -> x >= 1 end)
@spec any?(queue_type(), (any() -> boolean())) :: boolean()
Returns true if at least one element in enumerable is truthy.
Example:
new()
|> insert(1)
|> insert(2)
|> any?(fn x -> x >= 2 end)
@spec cons(queue_type(), any()) :: queue_type()
Inserts Item at the head of queue q1
. Returns the new queue q2
.
Example:
queue = cons(from_list([1, 2, 3]), 0)
to_list queue
# [0, 1, 2, 3]
@spec count(queue_type()) :: non_neg_integer()
For see more information, check len/1
@spec daeh(queue_type()) :: any()
Returns the tail item of a queue.
Example:
queue = daeh(from_list([1, 2, 3]))
# 3
Fails with reason empty if
queue
is empty. You must check if it is empty before tailing it.
Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:207: :queue.get_r({[], []})
iex:67: (file)
@spec delete(queue_type(), any()) :: queue_type()
Returns a copy of q1
where the first item matching item
is deleted, if there is such an item.
Example:
queue = from_list([1, 2, 3, 4, 5])
queue1 = queue |> delete(3)
member(queue1, 3)
# false
@spec delete_r(queue_type(), any()) :: queue_type()
Returns a copy of q1
where the last item matching item
is deleted, if there is such an item.
Example:
queue = from_list([1, 2, 3, 4, 3, 5])
queue1 = delete_r(queue, 3)
to_list(queue1).
[1,2,3,4,5]
@spec delete_with(queue_type(), (any() -> boolean())) :: queue_type()
Returns a copy of q1
where the first item for which fn
returns true is deleted,
if there is such an item.
Example:
queue = from_list([100,1, 2, 3, 4, 5])
queue1 = delete_with(queue, fn x -> x > 0)
to_list(queue1)
[1,2,3,4,5]
If we make it abstract in Elixir, you can do it with a list like this:
list = [1, 2, 3, 4, 5]
{before, [h | t]} = Enum.split_with(list, fn x -> x < 4 end)
new_list = before ++ t
@spec delete_with_r(queue_type(), (any() -> boolean())) :: queue_type()
Returns a copy of q1
where the last item for which fn
returns true is deleted,
if there is such an item.
Example:
queue = from_list([100,1, 4, 2, 3, 4, 5])
queue1 = delete_with_r(queue, fn x -> x == 4)
to_list(queue1)
[100,1, 4, 2, 3, 5]
@spec drop(queue_type()) :: queue_type()
Returns a queue q2
that is the result of removing the front item from q1
.
Example:
queue = from_list([1, 2, 3, 4, 5])
queue1 = drop(queue)
to_list(queue1)
# [2,3,4,5]
Fails with reason empty if
q1
is empty. You must check if it is empty before dropping it.
Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:252: :queue.drop({[], []})
iex:55: (file)
@spec drop_r(queue_type()) :: queue_type()
Returns a queue q2
that is the result of removing the rear item from q1
.
Example:
queue = from_list([1, 2, 3, 4, 5])
queue1 = drop_r(queue)
to_list(queue1)
# [1, 2, 3, 4]
Fails with reason empty if
q1
is empty. You must check if it is empty before dropping it.
Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:270: :queue.drop_r({[], []})
iex:58: (file)
@spec empty?(queue_type()) :: boolean()
@spec empty?(queue_type() | nil) :: boolean()
For see more information, check is_empty?/1
@spec filter(queue_type(), (any() -> boolean() | list())) :: queue_type()
Filters the enumerable, i.e. returns only those elements for which fun returns a truthy value.
It is like Enum.filter/2
.
Example:
queue = from_list([1, 2, 3, 4, 5])
queue1 = filter(queue, fn x -> x > 2 end)
to_list(queue1)
# [3, 4, 5]
From Erlang docs:
So,
Fun(Item)
returning[Item]
is thereby semantically equivalent to returning true, just as returning[]
is semantically equivalent to returning false.But returning a list builds more garbage than returning an atom.
Example:
queue = from_list([1, 2, 3, 4, 5])
# {[5, 4, 3], [1, 2]}
queue1 = filter(queue, fn x -> [x, x+1] end)
# {[6, 5, 5, 4, 4, 3], [1, 2, 2, 3]}
to_list(queue1)
# [1, 2, 2, 3, 3, 4, 4, 5, 5, 6]
@spec filtermap(queue_type(), (any() -> boolean() | {true, any()})) :: queue_type()
From Erlang doc:
Returns a queue q2
that is the result of calling Fun(Item)
on all items in q1
.
If Fun(Item)
returns true, Item is copied to the result queue. If it returns false,
Item is not copied.
If it returns {true, NewItem}
, the queue element at this position is replaced with NewItem
in the result queue.
Example:
queue = from_list([1, 2, 3, 4, 5])
queue1 = filtermap(queue, fn x -> x > 2 end)
to_list(queue1)
# [3, 4, 5]
@spec first(queue_type()) :: any()
Please see head/1
.
Invokes fun for each element in the enumerable with the accumulator. It is like Enum.reduce/3
.
From Erlang docs:
Calls Fun(Item, AccIn) on successive items Item of Queue, starting with AccIn == Acc
0.
The queue is traversed in queue order, that is, from front to rear.
Fun/2
must return a new accumulator, which is passed to the next call.
The function returns the final value of the accumulator. Acc0
is returned if the queue is empty.
Example:
queue = from_list([1, 2, 3, 4, 5])
1> fold(queue, 0, fn item, acc -> item + acc end)
# 15
2> fold(queue, 0, fn item, acc -> item * acc end)
# 120
@spec from_list(list()) :: queue_type()
Returns a queue containing the items in the same order; the head item of the list becomes the front item of the queue.
Example:
from_list([1, 2, 3, 4, 5])
# {[5, 4, 3], [1, 2]}
@spec get(queue_type()) :: any()
Returns Item at the front of a queue.
Example:
queue = from_list([1, 2, 3, 4, 5])
1 == get(queue)
Fails with reason empty if
queue
is empty. You must check if it is empty before getting it.
Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:188: :queue.get({[], []})
iex:58: (file)
@spec get_r(queue_type()) :: any()
Returns Item at the rear of a queue.
Example:
queue = from_list([1, 2, 3, 4, 5])
5 == get_r(queue)
Fails with reason empty if
queue
is empty. You must check if it is empty before getting it.
Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:207: :queue.get_r({[], []})
iex:58: (file)
@spec head(queue_type()) :: any()
Returns the head item of a queue.
Example:
queue = head(from_list([1, 2, 3]))
# 1
Fails with reason empty if
queue
is empty. You must check if it is empty before heading it.
Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:662: :queue.head({[], []})
iex:67: (file)
@spec in_r(queue_type(), any()) :: queue_type()
For see more information, check insert_r/2
@spec init(queue_type()) :: queue_type()
Returns a queue q2
that is the result of removing the tail item from q1
.
Example:
init(from_list([1, 2, 3]))
# {[2],[1]}
Fails with reason empty if
queue
is empty. You must check if it is empty before initing it.
Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:270: :queue.drop_r({[], []})
iex:67: (file)
@spec insert(queue_type(), any()) :: queue_type()
Inserts Item at the rear of queue q1
. Returns the resulting queue q2
.
Example:
queue = from_list([1, 2, 3, 4, 5])
queue1 = insert(queue, 100)
to_list(queue1)
# [1, 2, 3, 4, 5, 100]
@spec insert_r(queue_type(), any()) :: queue_type()
Inserts Item at the front of queue q1
. Returns the resulting queue q2
.
Example:
queue = from_list([1, 2, 3, 4, 5])
queue1 = insert_r(queue, 100)
to_list(queue1)
# [100, 1, 2, 3, 4, 5]
@spec is_empty?(queue_type() | nil) :: boolean()
Tests if a queue is empty and returns true if so, otherwise false.
Example:
queue = from_list([1, 2, 3, 4, 5])
is_empty?(queue)
# false
@spec is_queue?(queue_type()) :: boolean()
Tests if an entry is queue and returns true if so, otherwise false.
Example:
queue = from_list([1, 2, 3, 4, 5])
is_queue?(queue)
# true
@spec join(queue_type(), queue_type()) :: queue_type()
Returns a queue q3
that is the result of joining q1
and q2
with q1
in front of q2
.
Example:
queue = from_list([1, 2])
queue1 = from_list([3, 4])
join(queue, queue1)
# [1, 2, 3, 4]
@spec join_to_list(queue_type(), queue_type()) :: [any()]
@spec join_to_list([any()], [any()]) :: queue_type()
@spec last(queue_type()) :: any()
Returns the tail item of a queue.
Example:
last(from_list([1,2 , 3]))
# 3
Fails with reason empty if
queue
is empty. You must check if it is empty before getting it.
Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:207: :queue.get_r({[], []})
iex:67: (file)
@spec len(queue_type()) :: non_neg_integer()
Calculates and returns the length of a queue.
Example:
queue = from_list([1, 2])
len(queue)
# 2
@spec liat(queue_type()) :: queue_type()
@spec liat(queue_type()) :: queue_type()
It is like init/1
.
@spec member?(queue_type(), any()) :: boolean()
Checks if element exists within the queue.
Example:
queue = from_list([1, 2])
member?(queue, 2)
# true
@spec new() :: queue_type()
Returns an empty queue.
Example:
new()
# {[], []}
@spec out(queue_type()) :: {:empty, {[], []}} | {{:value, any()}, queue_type()}
Removes the item at the front of queue q1
. Returns tuple {{:value, item}, q2}
,
where Item is the item removed and q2
is the resulting queue.
If q1
is empty, tuple {empty, q1}
is returned.
Example:
queue = from_list([1, 2, 3, 4, 5])
# {[5, 4, 3],[1, 2]}
{{:value, 1}, new_queue} = out(queue)
# {{:value, 1}, {[5, 4, 3], [2]}}
to_list(new_queue)
# [2, 3, 4, 5]
@spec out_r(queue_type()) :: {:empty | {:value, any()}, queue_type()}
Removes the item at the rear of queue q1
. Returns tuple {{:value, item}, q2}
,
where Item is the item removed and q2
is the resulting queue.
If q1
is empty, tuple {empty, q1}
is returned.
Example:
queue = from_list([1, 2, 3, 4, 5])
# {[5,4,3], [1, 2]}
{{:value, 5}, new_queue} = out_r(queue)
# {{:value, 5}, {[5, 4, 3], [2, 1]}}
to_list(new_queue)
# [1, 2, 3, 4]
@spec peek(queue_type()) :: :empty | {:value, any()}
Returns tuple {:value, item}
, where Item is the front item of a queue,
or empty if a queue is :empty
.
Example:
peek(new())
# :empty
queue = from_list([1, 2 ,3 ,4 ,5])
# {[5, 4, 3], [1, 2]}
peek(queue)
# {:value, 1}
@spec peek_r(queue_type()) :: :empty | {:value, any()}
Returns tuple {:value, item}
, where Item is the rear item of a queue,
or empty if a queue is :empty
.
Example:
peek_r(new())
# :empty
queue = from_list([1, 2 ,3 ,4 ,5])
# {[5, 4, 3], [1, 2]}
peek_r(queue)
# {:value, 5}
@spec queue?(queue_type()) :: boolean()
For see more information, check is_queue?/1
For see more information, check fold/3
@spec reverse(queue_type()) :: queue_type()
Returns a queue q2
containing the items of q1
in the reverse order.
Example:
queue = from_list([1, 2, 3, 4, 5])
reverse(queue)
# {[1, 2], [5, 4, 3]}
@spec snoc(queue_type(), any()) :: queue_type()
Inserts Item as the tail item of a queue q1
. Returns a new queue like q2
.
Example:
queue = snoc(from_list([1, 2, 3]), 4)
# {[4, 3, 2], [1]}
to_list(queue)
# [1,2,3,4]
@spec split(queue_type(), non_neg_integer()) :: {queue_type(), queue_type()}
Splits q1
in two. The n
front items are put in q2
and the rest in q3
.
Example:
queue = from_list([1, 2, 3, 4, 5])
split(queue, 2)
# {{[2], [1]}, {[5], [3, 4]}}
@spec tail(queue_type()) :: queue_type()
Returns a queue q2
that is the result of removing the head item from q1
.
Fails with reason empty if
queue
is empty. You must check if it is empty before tailing it.
### Error output:
** (ErlangError) Erlang error: :empty
(stdlib 5.2.1) queue.erl:252: :queue.drop({[], []})
iex:67: (file)
@spec to_list(queue_type()) :: list()
Returns a list of the items in the queue in the same order; the front item of the queue becomes the head of the list.
Example:
queue = from_list([1, 2, 3, 4, 5])
# {[5, 4, 3], [1, 2]}
to_list(queue)
# [1, 2, 3, 4, 5]