package: pfds

module: queue

The queue module implements an immutable FIFO data structure. This module exports a single function make for creating a new queue.

queue.make


queue.make(init_seq = [])
      

Return a new queue initialized with the values from the sequence init_seq, which can be any object that implements the first and rest generic functions.

The new queue is represented as a closure that can respond to the following messages:


head()
      

Return the value at the front of the queue. Raise an error if the queue is empty.

    
tail()
      

Return a new queue with the front item removed. Raise an error if the queue is empty.


snoc(x)
      

Return a new queue with the element x added to its back.


is_empty()
      

Return true if the queue is empty, return false otherwise.

Examples:


let q = queue.make([1, 2])
q.head()
// 1
q.tail().head()
// 2
q.tail().tail().is_empty()
// true
q = q.snoc(3)
q.tail().tail().is_empty()
// false
q.tail().tail().head()
// 3          
      

Index | Packages Index