Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tables, sets, heaps, and queues

Tables

Table/new creates a mutable hash table. Its public operations place the table last, including mutating operations:

(let scores (Table/new))
(Table/set! "Ada" 10 scores)
(Table/set! "Lin" 20 scores)

(Table/has? "Ada" scores)
(Table/get "Ada" scores)

Table/get returns the table’s entry-vector form. Table/get* returns an (Option V) for a checked lookup; use Table/get-unsafe when presence is already established:

(match (Table/get* "Ada" scores)
  None 0
  (Some score) score)

(if (Table/has? "Ada" scores)
    (Table/get-unsafe "Ada" scores)
    0)

Table/update! and Table/update-or! cover common count/accumulator patterns; Table/entries, Table/keys, Table/values, and Table/count expose its contents. Tables support their established key representations, including strings and closed enum tags. Use Table/new/capacity when an approximate entry count is known, and Table/compact! to rebuild after substantial deletion.

Sets

Set/new, Set/add!, Set/remove!, Set/has?, Set/values, and set algebra are provided for the library’s set representation. As with tables, query the specific function type for the accepted key kind:

que --lib types 'Set/*'

Set algebra

Set/intersection, Set/union, Set/difference, and Set/xor each create a new set; neither input set is changed. Let A = {"red", "blue", "green"} and B = {"green", "gold"}:

Venn diagrams for intersection, union, difference, and xor
OperationMathematical meaningWith A and B
(Set/intersection A B)values in both sets, A ∩ B{"green"}
(Set/union A B)values in either set, A ∪ B{"red", "blue", "green", "gold"}
(Set/difference A B)values in the first but not the second, A \ B{"red", "blue"}
(Set/xor A B)values in exactly one input, (A \ B) ∪ (B \ A){"red", "blue", "gold"}
(let A (Vector->Set ["red" "blue" "green"]))
(let B (Vector->Set ["green" "gold"]))

(let common (Set/intersection A B))
(let all (Set/union A B))
(let only-A (Set/difference A B))
(let exclusive (Set/xor A B))

{ (Set/has? "green" common)    ; true
  (Set/has? "red" common)      ; false
  (Set/has? "gold" all)        ; true
  (Set/has? "blue" only-A)     ; true
  (Set/has? "green" exclusive)} ; false

Set/difference is directional: swapping its arguments changes the result. Set/xor is symmetric, so (Set/xor A B) and (Set/xor B A) contain the same members. A set’s bucket layout does not promise a presentation order; if you need a deterministic displayed vector, convert with Set->Vector and sort the result.

Heap and deque

Heap/* implements a mutable binary heap using a caller-supplied comparison predicate. Que/* provides a deque-like structure backed by vectors, with operations such as Que/push!, Que/prepend!, Que/first, Que/last, and Que/deque!.

These structures favor explicit mutation and predictable representation over hidden GC behavior. Use the public namespace functions rather than relying on their internal vector layout.

Matrices

Matrix/new, Matrix/in-bounds?, Matrix/for/i, and related helpers cover nested-vector matrix work. Matrix coordinates are explicit Int values; use the bounds helper before dynamic access when static analysis cannot prove an index safe.