queue(3erl)
π queue(3erl) β Erlang Module Definition π
okay bestie, welcome to queue(3erl), straight out of the Erlang module definitions, and no cap this is one of those modules you'll use literally every day π«Ά
πΈ NAME πΈ
queue β abstract data type for FIFO queues. That's First-In-First-Out, bestie, the OG "no cutting the line" energy π
β¨ DESCRIPTION β¨
This module provides (double-ended) FIFO queues in an efficient manner. Double-ended meaning you can play with both ends, icon π
Heads up, gorgeous: all functions fail with reason badarg if arguments are of the wrong type β for example, queue arguments that aren't actually queues, indexes that aren't integers, and list arguments that aren't lists. And improper lists? Those cause internal crashes π Not cute. An index out of range for a queue also causes a failure with reason badarg.
Some functions, where noted, fail with reason empty for an empty queue. Those are marked, so you'll know π
The data representing a queue as used by this module is to be regarded as opaque by other modules. In abstract terms, the representation is a composite type of existing Erlang terms. See note on data types. Any code assuming knowledge of the format is running on thin ice π§π β like, do not do that.
All operations have an amortized O(1) running time, except all/2, any/2, delete/2, delete_r/2, delete_with/2, delete_with_r/2, filter/2, filtermap/2, fold/3, join/2, len/1, member/2, split/2 that have O(n). Why? To minimize the size of a queue and minimize the amount of garbage built by queue operations, the queues do not contain explicit length information β and that is why len/1 is O(n). If better performance for this particular operation is essential, it is easy for the caller to keep track of the length. Your call, bestie π
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. Same vibe, different nickname iykyk π
Entering at the front and exiting at the rear are reverse operations on the queue. π
This module has three sets of interface functions: the "Original API", the "Extended API", and the "Okasaki API". Three eras, three aesthetics, one module π
The "Original API" and the "Extended API" both use the mental picture of a waiting line of items. Both have reverse operations suffixed _r. π
The "Original API" item removal functions return compound terms with both the removed item and the resulting queue. The "Extended API" contains alternative functions that build less garbage and functions for just inspecting the queue ends. Also the "Okasaki API" functions build less garbage. Less garbage = happy BEAM β¨
The "Okasaki API" is inspired by "Purely Functional Data Structures" by Chris Okasaki. It regards queues as lists. This API is by many regarded as strange and avoidable π For example, many reverse operations have lexically reversed names, some with more readable but perhaps less understandable aliases. It's giving "I'm not like other APIs" and honestly? Respect π₯
π¦ DATA TYPES π¦
queue(Item)
As returned by new/0.
queue() = queue(term())
π ORIGINAL API π
π EXPORTS π
all(Pred, Q :: queue(Item)) -> boolean()
Types:
Pred = fun((Item) -> boolean())
Returns true if Pred(Item) returns true for all items Item in Q, otherwise false. It's the "everyone agree or we riot" function π
any(Pred, Q :: queue(Item)) -> boolean()
Types:
Pred = fun((Item) -> boolean())
Returns true if Pred(Item) returns true for at least one item Item in Q, otherwise false. One icon is enough to make it a party π
delete(Item, Q1) -> Q2
Types:
Item = T
Q1 = Q2 = queue(T)
T = term()
Returns a copy of Q1 where the first item matching Item is deleted, if there is such an item. If there isn't, no drama, you just get the copy πΈ
delete_r(Item, Q1) -> Q2
Types:
Item = T
Q1 = Q2 = queue(T)
T = term()
Returns a copy of Q1 where the last item matching Item is deleted, if there is such an item. Same thing but from the other end, the _r suffix strikes again π
delete_with(Pred, Q1) -> Q2
Types:
Pred = fun((Item) -> boolean())
Q1 = Q2 = queue(Item)
Item = term()
Returns a copy of Q1 where the first item for which Pred returns true is deleted, if there is such an item. Predicate-powered deletion, we love to see it π«Ά
delete_with_r(Pred, Q1) -> Q2
Types:
Pred = fun((Item) -> boolean())
Q1 = Q2 = queue(Item)
Item = term()
Returns a copy of Q1 where the last item for which Pred returns true is deleted, if there is such an item. Backwards edition π
filter(Fun, Q1 :: queue(Item)) -> Q2 :: queue(Item)
Types:
Fun = fun((Item) -> boolean() | [Item])
Returns a queue Q2 that is the result of calling Fun(Item) on all items in Q1.
So here's the tea β If Fun(Item) returns true, Item is copied to the result queue. If it returns false, Item is not copied. If it returns a list, the list elements are inserted instead of Item in the result queue. Glow-up or replacement, your choice β¨
So, Fun(Item) returning [Item] is thereby semantically equivalent to returning true, just as returning [] is semantically equivalent to returning false. But β and this is the important bit β returning a list builds more garbage than returning an atom. Efficiency queen behavior: return the atom π
filtermap(Fun, Q1) -> Q2
Types:
Fun = fun((Item) -> boolean() | {true, Value})
Q1 = queue(Item)
Q2 = queue(Item | Value)
Item = Value = term()
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. It's a filter AND a makeover in one pass π
π₯
fold(Fun, Acc0, Q :: queue(Item)) -> Acc1
Types:
Fun = fun((Item, AccIn) -> AccOut)
Acc0 = Acc1 = AccIn = AccOut = term()
Calls Fun(Item, AccIn) on successive items Item of Queue, starting with AccIn == Acc0. 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 β look at these sums and products go off:
> queue:fold(fun(X, Sum) -> X + Sum end, 0, queue:from_list([1,2,3,4,5])).
15
> queue:fold(fun(X, Prod) -> X * Prod end, 1, queue:from_list([1,2,3,4,5])).
120
from_list(L :: [Item]) -> queue(Item)
Returns a queue containing the items in L in the same order; the head item of the list becomes the front item of the queue. Glow-up from list to queue, slay π
in(Item, Q1 :: queue(Item)) -> Q2 :: queue(Item)
Inserts Item at the rear of queue Q1. Returns the resulting queue Q2. Get in line, bestie πΆββοΈ
in_r(Item, Q1 :: queue(Item)) -> Q2 :: queue(Item)
Inserts Item at the front of queue Q1. Returns the resulting queue Q2. Line-cutting but make it legal, because the front is also an exit π
is_empty(Q :: queue()) -> boolean()
Tests if Q is empty and returns true if so, otherwise false. Vibe check on the queue π«Ά
is_queue(Term :: term()) -> boolean()
Tests if Term is a queue and returns true if so, otherwise false. Note that the test will return true for a term coinciding with the representation of a queue, even when not constructed by this module. See also note on data types. So yeah, it's a look-alike check, not a birth certificate π
join(Q1 :: queue(Item), Q2 :: queue(Item)) -> Q3 :: queue(Item)
Returns a queue Q3 that is the result of joining Q1 and Q2 with Q1 in front of Q2. Merging friend groups, but orderly π
len(Q :: queue()) -> integer() >= 0
Calculates and returns the length of queue Q. Remember the earlier tea: this one is O(n) because the queue keeps no explicit length information π
member(Item, Q :: queue(Item)) -> boolean()
Returns true if Item matches some element in Q, otherwise false. Are you on the list or not π
new() -> queue()
Returns an empty queue. Fresh start, blank slate, main character energy β¨
out(Q1 :: queue(Item)) -> {{value, Item}, Q2 :: queue(Item)} | {empty, Q1 :: queue(Item)}
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. Serving the front of the line π«Ά
out_r(Q1 :: queue(Item)) -> {{value, Item}, Q2 :: queue(Item)} | {empty, Q1 :: queue(Item)}
Removes the item at the rear of queue Q1. Returns tuple {{value, Item}, Q2}, where Item is the item removed and Q2 is the new queue. If Q1 is empty, tuple {empty, Q1} is returned. Same serve, other end π
reverse(Q1 :: queue(Item)) -> Q2 :: queue(Item)
Returns a queue Q2 containing the items of Q1 in the reverse order. Flip it like it's hot π₯
split(N :: integer() >= 0, Q1 :: queue(Item)) -> {Q2 :: queue(Item), Q3 :: queue(Item)}
Splits Q1 in two. The N front items are put in Q2 and the rest in Q3. Divide and conquer, bestie βοΈ
to_list(Q :: queue(Item)) -> [Item]
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. The reverse glow-up of from_list, iconic π
π EXTENDED API π
π EXPORTS π
drop(Q1 :: queue(Item)) -> Q2 :: queue(Item)
Returns a queue Q2 that is the result of removing the front item from Q1.
Fails with reason empty if Q1 is empty. β οΈ
drop_r(Q1 :: queue(Item)) -> Q2 :: queue(Item)
Returns a queue Q2 that is the result of removing the rear item from Q1.
Fails with reason empty if Q1 is empty. β οΈ
get(Q :: queue(Item)) -> Item
Returns Item at the front of queue Q.
Fails with reason empty if Q is empty. β οΈ
get_r(Q :: queue(Item)) -> Item
Returns Item at the rear of queue Q.
Fails with reason empty if Q is empty. β οΈ
peek(Q :: queue(Item)) -> empty | {value, Item}
Returns tuple {value, Item}, where Item is the front item of Q, or empty if Q is empty. Just a lil look, no removal, no commitment π
peek_r(Q :: queue(Item)) -> empty | {value, Item}
Returns tuple {value, Item}, where Item is the rear item of Q, or empty if Q is empty. Same peek, other end π
π§ OKASAKI API π§
okay so the Okasaki functions are literally right here in the next part β this is the one everybody calls strange and avoidable, remember? π
π EXPORTS π
Okay bestie, this is the Okasaki-flavored batch of functions β same queue, same energy, just with the fancy names. Let's go one by one, no skips, no cap. π
cons(Item, Q1 :: queue(Item)) -> Q2 :: queue(Item)β insertsItemat the head of queueQ1. Returns the shiny new queueQ2. πdaeh(Q :: queue(Item)) -> Itemβ returns the tail item of queueQ. π And yeah, it's literallyheadspelled backwards, iykyk.
Fails with reason empty if Q is empty. π
head(Q :: queue(Item)) -> Itemβ returnsItemfrom the head of queueQ. π
Fails with reason empty if Q is empty. π
init(Q1 :: queue(Item)) -> Q2 :: queue(Item)β hands you a queueQ2that's the result of removing the tail item fromQ1. π¦
Fails with reason empty if Q1 is empty. π
lait(Q1 :: queue(Item)) -> Q2 :: queue(Item)β returns a queueQ2that's the result of removing the tail item fromQ1. π
Fails with reason empty if Q1 is empty. π
π¨ Bestie, listen: the name lait/1 is a misspelling β do not use it anymore. Like, fr fr, don't. π
last(Q :: queue(Item)) -> Itemβ returns the tail item of queueQ. β¨
Fails with reason empty if Q is empty. π
liat(Q1 :: queue(Item)) -> Q2 :: queue(Item)β returns a queueQ2that's the result of removing the tail item fromQ1. πΈ
Fails with reason empty if Q1 is empty. π
snoc(Q1 :: queue(Item), Item) -> Q2 :: queue(Item)β insertsItemas the tail item of queueQ1. Returns the new queueQ2. π₯ (Yes, it'sconsbackwards β the Okasaki girlies ate with this one.)tail(Q1 :: queue(Item)) -> Q2 :: queue(Item)β returns a queueQ2that's the result of removing the head item fromQ1. π§
Fails with reason empty if Q1 is empty. π
And that's the whole queue page, bestie β every function, every flag, every empty fail, all accounted for. π
Shoutout to Ericsson AB for this one, sitting pretty in stdlib 4.3.1.3 β that's the version tag and source credit from the page footer, so you know exactly where this lore came from. π«Ά Go forth and queue responsibly. πβ¨
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms