package: pfds

module: heap

The heap module implements an immutable priority queue. This module exports a single function make for creating a priority queue.

heap.make


heap.make(init_seq = [])
      

Return a new priority queue initialized with the values from the sequence init_seq, which can be any object that implements the first and rest generic functions. The elements added to the priority queue should be of the same type and must implement compare.

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


head()
      

Return the minimum value in the queue. Raise an error if the queue is empty.

    
tail()
      

Return a new queue with the minimum value removed. Raise an error if the queue is empty.


first
      

Return the minimum value in the priority queue. Raise an error if the queue is empty.

    
rest
      

Return a new priority queue with the minimum value removed. Return false if the queue is empty.


cons(x)
      

Return a new priority queue with the element x added.


is_empty()
      

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

Examples:


let q = heap.make([100, 2, 340])
q.head()
// 2
q.tail().head()
// 100
q = q.cons(0)
first(q)
// 0
      

Index | Packages Index