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.vecPurpose
Section titled “Purpose”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).
Conceptual model
Section titled “Conceptual model”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!Quickstart
Section titled “Quickstart”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) // truevec.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"Aggregates & folds (pure-ZZ)
Section titled “Aggregates & folds (pure-ZZ)”import std.vec
vec.sum([1,2,3]) // 6vec.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.0vec.concat([1,2], [3,4]) // [1,2,3,4]vec.flatten([[1,2],[3]]) // [1,2,3]vec.index_of([1,2,3], 2) // 1vec.last_index_of([1,2,2], 2) // 2vec.enumerate(["a","b"]) // [(0,"a"), (1,"b")]Method syntax
Section titled “Method syntax”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/reversereturn new arrays. Without re-binding, the original is unchanged.
Edge cases & gotchas
Section titled “Edge cases & gotchas”- Arrays are values.
vec.push(nums, 4)does not mutatenums. Re-bind:nums = vec.push(nums, 4)ornums = nums.push(4). sortrequires comparableT. For custom types, usesort_by/sort_key.sort_bycomparator contract: return-1/0/1(any negative/zero/positive works). Inconsistent comparators yield unspecified order.sort_keykeyKmust beint|str|float.min_val/max_valreturnOption— always handle.nonefor possibly-empty arrays.vec.appendwas removed. Usevec.push+ method formxs.push(x).- Index bounds:
insert/removewith out-of-range index are runtime errors.
Production patterns
Section titled “Production patterns”import std.vec
// Pipeline: filter even, double, sumnums := [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 defaultscores := [3, 1, 2]best := vec.min_val(scores).unwrap_or(0)API reference
Section titled “API reference”24 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
See also
Section titled “See also”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