Skip to content

std.vec - ZZ

Generic array operations over [T]. Arrays are values (copy-on-write under the hood): mutating a parameter mutates a copy, so you re-bind the result. All functions return a new array. vec.* short form works after import std.vec. Fully generic over element type T. Byte-identical on VM and AOT.

import std.vec

std.vec is the canonical home for array pipelines. It provides immutable-style operations (each returns a new array) plus method-call syntax ([1,2].push(3)). Because arrays are values, vec.push does not mutate in place — use xs = vec.push(xs, x) or xs = xs.push(x).

Arrays in ZZ are copy-on-write values. A vec.* call shares storage with the input until a write occurs, then copies (CoW). This makes chained operations cheap until the final result is materialized.

Method-call form (xs.push(x)) is sugar for std.vec.push(xs, x) and returns the new array — you must re-bind:

xs := [1, 2]
xs = xs.push(3) // [1, 2, 3] — re-bind required!
import std.vec
nums := [3, 1, 2]
vec.push(nums, 4) // [3, 1, 2, 4]
vec.pop(nums) // [3, 1]
vec.sort(nums) // [1, 2, 3]
vec.reverse(nums) // [2, 1, 3]
vec.join(nums, ", ") // "3, 1, 2"
vec.contains(nums, 1) // true
vec.insert(nums, 0, 99) // [99, 3, 1, 2]
vec.remove(nums, 1) // [3, 2]
// Method syntax (re-bind!)
[3, 1, 2].sort() // [1, 2, 3]
[3, 1, 2].push(4) // [3, 1, 2, 4]
[3, 1, 2].contains(1) // true
["a", "b"].join(", ") // "a, b"
import std.vec
vec.sum([1,2,3]) // 6
vec.product([1,2,3]) // 6
_ := vec.min_val([3,1,2]) // .some(1)
_ := vec.max_val([3,1,2]) // .some(3)
vec.sum_f([1.5, 2.5]) // 4.0 (Kahan-compensated)
vec.product_f([2.0, 3.0]) // 6.0
vec.concat([1,2], [3,4]) // [1,2,3,4]
vec.flatten([[1,2],[3]]) // [1,2,3]
vec.index_of([1,2,3], 2) // 1
vec.last_index_of([1,2,2], 2) // 2
vec.enumerate(["a","b"]) // [(0,"a"), (1,"b")]
import std.vec
[3, 1, 2].sort() // [1, 2, 3]
[3, 1, 2].reverse() // [2, 1, 3]
[3, 1, 2].push(4) // [3, 1, 2, 4] — re-bind: xs = xs.push(4)
[3, 1, 2].contains(1) // true
["a", "b"].join(", ") // "a, b"
["a","b"].enumerate() // [(0,"a"),(1,"b")]

Important: push/pop/insert/remove/sort/reverse return new arrays. Without re-binding, the original is unchanged.

  • Arrays are values. vec.push(nums, 4) does not mutate nums. Re-bind: nums = vec.push(nums, 4) or nums = nums.push(4).
  • sort requires comparable T. For custom types, use sort_by/sort_key.
  • sort_by comparator contract: return -1/0/1 (any negative/zero/positive works). Inconsistent comparators yield unspecified order.
  • sort_key key K must be int|str|float.
  • min_val/max_val return Option — always handle .none for possibly-empty arrays.
  • vec.append was removed. Use vec.push + method form xs.push(x).
  • Index bounds: insert/remove with out-of-range index are runtime errors.
import std.vec
// Pipeline: filter even, double, sum
nums := [1,2,3,4,5,6]
evens := filter(nums, |n| n % 2 == 0) // [2,4,6]
doubled := map(evens, |n| n * 2) // [4,8,12]
result := vec.sum(doubled) // 24
// Safe min with default
scores := [3, 1, 2]
best := vec.min_val(scores).unwrap_or(0)

24 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.vec.concat concat(a: [int], b: [int]) -> [int] Concatenate two int arrays
std.vec.contains contains<T>(v: [T], x: T) -> bool Element exists (equality)
std.vec.enumerate enumerate<T>(v: [T]) -> [(int, T)] Index + value pairs
std.vec.flatten flatten(arr: [[int]]) -> [int] Flatten one level
std.vec.fold fold(arr: [int], init: int) -> int Left fold
std.vec.index_of index_of(arr: [int], target: int) -> int First index; -1 on miss
std.vec.insert insert<T>(v: [T], idx: int, x: T) -> [T] Insert at index
std.vec.join join<T>(v: [T], sep: str) -> str Join elements as string
std.vec.last_index_of last_index_of(arr: [int], target: int) -> int Last index; -1 on miss
std.vec.len len<T>(v: [T]) -> int Array length
std.vec.max_val max_val<T>(arr: [T]) -> Option<T> Maximum or .none if empty
std.vec.min_val min_val<T>(arr: [T]) -> Option<T> Minimum or .none if empty
std.vec.pop pop<T>(v: [T]) -> [T] New array without last element
std.vec.product product(arr: [int]) -> int Product of ints
std.vec.product_f product_f(arr: [float]) -> float Float product
std.vec.push push<T>(v: [T], x: T) -> [T] New array with x appended
std.vec.remove remove<T>(v: [T], idx: int) -> [T] Remove at index
std.vec.reverse reverse<T>(v: [T]) -> [T] Reversed copy
std.vec.sort sort<T>(v: [T]) -> [T] Sorted copy (natural order)
std.vec.sort_by sort_by<T>(v: [T], cmp: func(T, T) -> int) -> [T] Comparator ordering (-1/0/1)
std.vec.sort_by_desc sort_by_desc<T>(v: [T], cmp: func(T, T) -> int) -> [T] Descending comparator
std.vec.sort_key sort_key<T,U>(v: [T], key: func(T) -> U) -> [T] Key-based ordering (K: int\
std.vec.sum sum(arr: [int]) -> int Sum of ints
std.vec.sum_f sum_f(arr: [float]) -> float Kahan-compensated float sum
  • Built-ins — Everything that needs no import: print, len, range, map, int, str and the assert helpers.
  • std.bytes — Contiguous byte buffers: builders, length, extension and UTF-8 conversion.

Full index: API reference · Standard library