Module imm/vec

imm/vec

Persistent immutable vector with efficient indexed access and append.

Vec(T) is an immutable vector backed by atomic object for thread-safe structural sharing. All mutations return new vectors, leaving the original unchanged.

All elements must implement Send to guarantee thread safety.

Examples

{ Vec } :: import "std/imm/vec";

v := Vec(i32).new();
v = v.push(i32(1)).push(i32(2)).push(i32(3));
assert((v.get(usize(0)).unwrap() == i32(1)), "first element");
assert((v.len() == usize(3)), "length is 3");

Types

Vec type-function
fn(comptime(T) : Type) -> (comptime(fn_return_yo00cb0877_id_17) : Type)

Persistent immutable vector.

Backed by a shared flat array with copy-on-write semantics. All mutations create a new vector, sharing the backing buffer via atomic reference counting when possible.

Type Parameters

NameTypeNotes
TTypecomptime

Trait Implementations

impl(forall(T : Type), where(T <: Send), Vec(T), Dispose(...))
dispose : (fn(self: Self) -> unit)

Returns: unit

impl(forall(T : Type), where(T <: Send), Vec(T), ...)
_raw_alloc : (fn(cap: usize) -> *(T))

Allocate raw buffer without creating an intermediate atomic object.

Returns: *(T)

_copy_elems : (fn( dst: *(T), src: *(T), dst_offset: usize, src_offset: usize, count: usize ) -> unit)

Copy elements element-by-element so RC-backed values duplicate correctly.

Returns: unit

new : (fn() -> Self)

Create a new empty vector.

Returns: Self

with_capacity : (fn(cap: usize) -> Self)

Create a new empty vector with the given initial capacity.

Returns: Self

len : (fn(self: Self) -> usize)

Number of elements in the vector.

Returns: usize

is_empty : (fn(self: Self) -> bool)

Check if the vector is empty.

Returns: bool

get : (fn(self: Self, idx: usize) -> Option(T))

Get the element at idx, or .None if out of bounds.

Returns: Option(T)

set : (fn(own(self): Self, idx: usize, val: T) -> Self)

Return a new vector with the element at idx replaced by val. Panics if idx >= len.

Returns: Self

push : (fn(own(self): Self, val: T) -> Self)

Return a new vector with val appended.

Returns: Self

first : (fn(self: Self) -> Option(T))

Return the first element, or .None if empty.

Returns: Option(T)

last : (fn(self: Self) -> Option(T))

Return the last element, or .None if empty.

Returns: Option(T)

pop : (fn(own(self): Self) -> PopResult(T))

Return a new vector without the last element, and the removed element.

Returns: PopResult(T)

slice : (fn(self: Self, start: usize, end: usize) -> Self)

Return a new vector containing only elements in range [start, end).

Returns: Self

concat : (fn(own(self): Self, other: Self) -> Self)

Return a new vector that is the concatenation of self and other.

Returns: Self

reverse : (fn(own(self): Self) -> Self)

Return a new vector with elements reversed.

Returns: Self

map : (fn(forall(U : Type), self: Self, f: Impl(Fn(a: T) -> U), where(U <: Send)) -> Vec(U))

Apply a function to each element, producing a new vector.

Returns: Vec(U)

filter : (fn(self: Self, f: Impl(Fn(a: T) -> bool)) -> Self)

Filter elements by a predicate, returning a new vector.

Returns: Self

fold : (fn(forall(U : Type), self: Self, init: U, f: Impl(Fn(acc: U, elem: T) -> U)) -> U)

Left fold over elements.

Returns: U

any : (fn(self: Self, f: Impl(Fn(a: T) -> bool)) -> bool)

Check if any element satisfies the predicate.

Returns: bool

all : (fn(self: Self, f: Impl(Fn(a: T) -> bool)) -> bool)

Check if all elements satisfy the predicate.

Returns: bool

find : (fn(self: Self, f: Impl(Fn(a: T) -> bool)) -> Option(T))

Find the first element satisfying the predicate.

Returns: Option(T)

index_of : (fn(self: Self, val: T, where(T <: Eq(T))) -> Option(usize))

Return the index of the first element equal to val, or .None.

Returns: Option(usize)

contains : (fn(self: Self, val: T, where(T <: Eq(T))) -> bool)

Check if the vector contains val.

Returns: bool

dedup : (fn(own(self): Self, where(T <: Eq(T))) -> Self)

Return a new vector with duplicate elements removed (preserves first occurrence).

Returns: Self

zip_with : (fn(forall(U : Type, V : Type), self: Self, other: Vec(U), f: Impl(Fn(a: T, b: U) -> V), where(U <: Send, V <: Send)) -> Vec(V))

Zip two vectors together using a combining function.

Returns: Vec(V)

from_slice : (fn(s: Slice(T)) -> Self)

Create a vector from a slice, copying all elements.

Returns: Self

impl(forall(T : Type), where(T <: (Send, Eq(T))), Vec(T), Eq(Vec(T))(...))
PopResult type-function
fn(comptime(T) : Type) -> (comptime(fn_return_yo00cb0877_id_84) : Type)

Result of a pop operation.

Type Parameters

NameTypeNotes
TTypecomptime