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

seq

lib/seq.vk

lib/seq.vk Pure-Veko functional & in-place sequence algorithms (merge sort, binary search, slicing, grouping). Zero capabilities required.

signature
reverse = fn [xs block] block

Returns a new block containing the elements of xs in reverse order.

Executable Example
veko
use [seq]
if (seq/reverse [1 2 3]) != [3 2 1] [ err 'reverse' ] else [ .ok ]
take fn
signature
take = fn [xs block, k i64] block

Returns the first k elements of xs (clamped to [0, len xs]).

drop fn
signature
drop = fn [xs block, k i64] block

Drops the first k elements of xs and returns the remainder.

fold fn
signature
fold = fn [xs block, init any, step fn[any,any->any]] any

Left-folds xs starting from init using step = fn [acc, elem] [ ... ].

Executable Example
veko
use [seq]
total = seq/fold [1 2 3 4] 0 fn [acc, x] [ acc + x ]
if total != 10 [ err 'fold' ] else [ .ok ]
any? fn
signature
any? = fn [xs block, pred fn[any->bool]] bool

Returns true if pred holds for at least one element in xs.

all? fn
signature
all? = fn [xs block, pred fn[any->bool]] bool

Returns true if pred holds for every element in xs.

find fn
signature
find = fn [xs block, pred fn[any->bool]] any

Returns the first element matching pred, or err 'seq/find: not found' (combine with /or).

signature
partition = fn [xs block, pred fn[any->bool]] map

Splits xs into { yes = [...], no = [...] } according to pred.

signature
unique = fn [xs block] block

Removes duplicate scalar/hashable elements from xs, preserving first-seen order.

Executable Example
veko
use [seq]
if (seq/unique [3 1 3 2 1]) != [3 1 2] [ err 'unique' ] else [ .ok ]
signature
chunk = fn [xs block, size i64] block

Splits xs into consecutive chunks of length size (last chunk may be shorter).

zip fn
signature
zip = fn [a block, b block] block

Zips two blocks a and b into pairs [ [a0 b0] [a1 b1] ... ] up to min(len a, len b).

signature
enumerate = fn [xs block] block

Produces [ [0 xs[0]] [1 xs[1]] ... ].

signature
flatten = fn [xs block] block

Flattens one level of nested blocks into a single block.

signature
group-by = fn [xs block, key_fn fn[any->str]] map

Groups elements of xs into a map keyed by (key-fn elem).

signature
count-by = fn [xs block, key_fn fn[any->str]] map

Counts elements of xs per key produced by (key-fn elem).

signature
range-seq = fn [start i64, stop i64, step i64] block[i64]

Generates an integer sequence [start, start + step, ...] stopping before stop.

signature
sort-by = fn [xs block, cmp fn[any,any->i64]] block

Stable O(N log N) merge sort using comparator cmp = fn [a, b] i64 [ ... ] (< 0, 0, > 0).

Executable Example
veko
use [seq]
s = seq/sort-by [5 1 4 2 3] fn [a, b] [ a - b ]
if s != [1 2 3 4 5] [ err 'sort-by' ] else [ .ok ]
sort fn
signature
sort = fn [xs block] block

Stable O(N log N) ascending sort for ordered scalars (i64, f64, or str).

Executable Example
veko
use [seq]
if (seq/sort [9 2 7 1]) != [1 2 7 9] [ err 'sort' ] else [ .ok ]