veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

coll

lib/coll.vk

lib/coll.vk Pure-Veko collections: hashed Set, bounded Ring buffer, binary min-Heap, LRU cache, and Memo table. Built on Veko 1.0's hybrid hashed map and O(1) in-place put. Zero capabilities required.

Set struct
signature
Set = struct { data map = ... }

Unordered set of scalar keys (str, i64, or .enum), backed by hybrid hashed map.

signature
set-new = fn [] Set

Creates a new empty Set.

Executable Example
veko
use [coll]
mut s = coll/set-new
coll/set-add mut s 'alpha'
coll/set-add mut s 'beta'
if (coll/set-len s) != 2 or (not (coll/set-has? s 'alpha')) [ err 'set' ] else [ .ok ]
signature
set-from = fn [xs block] Set

Creates a Set initialized with all elements of xs.

signature
set-add = fn [mut s Set, k any] i64

Inserts k into s in-place.

signature
set-remove = fn [mut s Set, k any] i64

Removes k from s in-place.

signature
set-has? = fn [s Set, k any] bool

Returns true if k is in s.

signature
set-len = fn [s Set] i64

Returns the number of elements in s.

signature
set-items = fn [s Set] block

Returns all elements of s as a block.

signature
set-union = fn [a Set, b Set] Set

Returns the union a U b.

signature
set-intersect = fn [a Set, b Set] Set

Returns the intersection a /\ b.

signature
set-diff = fn [a Set, b Set] Set

Returns the difference a \ b.

Ring struct
signature
Ring = struct { buf block, cap i64, head i64 = ..., count i64 = ... }

Fixed-capacity O(1) FIFO ring buffer.

signature
ring-new = fn [cap i64] Ring

Creates a new bounded Ring with capacity cap > 0.

Executable Example
veko
use [coll]
mut r = coll/ring-new 3
_ = coll/ring-push mut r 10
_ = coll/ring-push mut r 20
v = coll/ring-pop mut r
if v != 10 or (coll/ring-len r) != 1 [ err 'ring' ] else [ .ok ]
signature
ring-len = fn [r Ring] i64

Number of items currently stored in r.

signature
ring-full? = fn [r Ring] bool

Returns true if r is at capacity.

signature
ring-empty? = fn [r Ring] bool

Returns true if r is empty.

signature
ring-push = fn [mut r Ring, val any] i64

Appends val to the tail of r; returns err if full.

signature
ring-pop = fn [mut r Ring] any

Removes and returns the oldest item from r; returns err if empty.

signature
ring-peek = fn [r Ring] any

Returns the oldest item in r without removing it; returns err if empty.

Heap struct
signature
Heap = struct { items block = ... }

Binary min-heap priority queue storing { prio = i64, val = any } entries.

signature
heap-new = fn [] Heap

Creates a new empty min-Heap.

Executable Example
veko
use [coll]
mut h = coll/heap-new
_ = coll/heap-push mut h 30 'c'
_ = coll/heap-push mut h 10 'a'
_ = coll/heap-push mut h 20 'b'
first_v = coll/heap-pop mut h
if first_v != 'a' [ err 'heap' ] else [ .ok ]
signature
heap-len = fn [h Heap] i64

Returns the number of entries in h.

signature
heap-push = fn [mut h Heap, prio i64, val any] i64

Pushes val with integer priority prio (smaller prio pops first).

signature
heap-peek = fn [h Heap] any

Returns the lowest-priority value without popping; returns err if empty.

signature
heap-pop = fn [mut h Heap] any

Pops and returns the value with the smallest priority; returns err if empty.

Lru struct
signature
Lru = struct { cap i64, data map = ..., order block[str] = ... }

Bounded Least-Recently-Used cache keyed by str.

signature
lru-new = fn [cap i64] Lru

Creates a new LRU cache holding at most cap > 0 entries.

Executable Example
veko
use [coll]
mut c = coll/lru-new 2
coll/lru-put mut c 'a' 1
coll/lru-put mut c 'b' 2
_ = coll/lru-get mut c 'a'
coll/lru-put mut c 'c' 3
if (coll/lru-has? c 'b') or (not (coll/lru-has? c 'a')) [ err 'lru' ] else [ .ok ]
signature
lru-has? = fn [c Lru, k str] bool

Returns true if k is currently cached in c.

signature
lru-len = fn [c Lru] i64

Returns the number of cached entries in c.

signature
lru-get = fn [mut c Lru, k str] any

Looks up k in c, marking it most-recently-used; returns err if absent.

signature
lru-put = fn [mut c Lru, k str, val any] i64

Inserts or updates k -> val in c, evicting the least-recently-used key when full.

Memo struct
signature
Memo = struct { cache map = ..., hits i64 = ... }

Simple key-value memoization cache for pure functions.

signature
memo-new = fn [] Memo

Creates a new empty Memo cache.

signature
memo-call = fn [mut m Memo, key str, compute fn[->any]] any

Returns cached value for key, or invokes (compute) once, caches the result, and returns it.