seq
lib/seq.vklib/seq.vk Pure-Veko functional & in-place sequence algorithms (merge sort, binary search, slicing, grouping). Zero capabilities required.
reverse = fn [xs block] block
Returns a new block containing the elements of xs in reverse order.
use [seq]
if (seq/reverse [1 2 3]) != [3 2 1] [ err 'reverse' ] else [ .ok ]
fold = fn [xs block, init any, step fn[any,any->any]] any
Left-folds xs starting from init using step = fn [acc, elem] [ ... ].
use [seq]
total = seq/fold [1 2 3 4] 0 fn [acc, x] [ acc + x ]
if total != 10 [ err 'fold' ] else [ .ok ]
any? = fn [xs block, pred fn[any->bool]] bool
Returns true if pred holds for at least one element in xs.
all? = fn [xs block, pred fn[any->bool]] bool
Returns true if pred holds for every element in xs.
find = fn [xs block, pred fn[any->bool]] any
Returns the first element matching pred, or err 'seq/find: not found' (combine with /or).
partition = fn [xs block, pred fn[any->bool]] map
Splits xs into { yes = [...], no = [...] } according to pred.
unique = fn [xs block] block
Removes duplicate scalar/hashable elements from xs, preserving first-seen order.
use [seq]
if (seq/unique [3 1 3 2 1]) != [3 1 2] [ err 'unique' ] else [ .ok ]
chunk = fn [xs block, size i64] block
Splits xs into consecutive chunks of length size (last chunk may be shorter).
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).
group-by = fn [xs block, key_fn fn[any->str]] map
Groups elements of xs into a map keyed by (key-fn elem).
count-by = fn [xs block, key_fn fn[any->str]] map
Counts elements of xs per key produced by (key-fn elem).
range-seq = fn [start i64, stop i64, step i64] block[i64]
Generates an integer sequence [start, start + step, ...] stopping before stop.
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).
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 [xs block] block
Stable O(N log N) ascending sort for ordered scalars (i64, f64, or str).
use [seq]
if (seq/sort [9 2 7 1]) != [1 2 7 9] [ err 'sort' ] else [ .ok ]
binary-search = fn [xs block, target any] i64
Binary search on a sorted block xs; returns index >= 0 if found or -1 otherwise.
use [seq]
if (seq/binary-search [10 20 30 40 50] 30) != 2 [ err 'binary-search' ] else [ .ok ]