Queue (std)
std/queue is written in Agentica: import Queue from "std/queue".
A queue is an immutable Queue.QueueState value. Every function gives a new queue; assign it to keep it, or continue with Popped.remaining. Inputs are never changed, and empty strings and duplicates are valid. push adds at the back and pop removes the oldest value; pop and peek on an empty queue return an error Result.
The queue holds only String values, and there are no blocking cross-task operations. push, pop, peek, size and empty? are O(n) with the current list operations; no constant-time FIFO is promised.
Queue.new #
Queue.new() -> Queue.QueueStateimport Queue from "std/queue"
def main() -> Void
q = Queue.new()
puts(Queue.size(q))
end0Queue.push #
Queue.push(queue: Queue.QueueState, value: String) -> Queue.QueueStateAdds a value at the back.
import Queue from "std/queue"
def main() -> Void
q = Queue.push(Queue.new(), "a")
q = Queue.push(q, "b")
puts(Queue.size(q))
end2Queue.pop #
Queue.pop(queue: Queue.QueueState) -> Result[Queue.Popped]Takes the oldest value. It gives a Popped with value and the remaining queue, or an error when the queue is empty.
import Queue from "std/queue"
def main() -> Void
q = Queue.push(Queue.push(Queue.new(), "a"), "b")
popped = Queue.pop(q).unwrap!
puts(popped.value)
puts(Queue.size(popped.remaining))
puts(Queue.pop(Queue.new()).ok?)
enda
1
falseQueue.peek #
Queue.peek(queue: Queue.QueueState) -> Result[String]Reads the oldest value without removing it.
import Queue from "std/queue"
def main() -> Void
q = Queue.push(Queue.new(), "first")
puts(Queue.peek(q))
endok("first")Queue.size and Queue.empty? #
Queue.size(queue: Queue.QueueState) -> Int
Queue.empty?(queue: Queue.QueueState) -> Boolimport Queue from "std/queue"
def main() -> Void
q = Queue.new()
puts(Queue.empty?(q))
q = Queue.push(q, "x")
puts(Queue.empty?(q))
puts(Queue.size(q))
endtrue
false
1