@immut.Vector
This page tracks the current repository implementation and is written as the 0.3.0 API baseline.
@immut.Vector[T]
moonbit
struct Vector[T] {
data : VecCore[T]
} derive(Eq)- Description Represents an immutable vector. Internally it is backed by
VecCore[T], the package alias for the core persistent vector storage. The public library alias for this abstraction isVecLib[T].
Semantic Notes
@immut.Vectoruses value semantics consistently: operations such asset,map,left_scale, andright_scalereturn new values.This package is intended to stay aligned with the shared algebraic subset exposed by
@mutable.Vector, while intentionally excluding in-place mutation APIs.The optimized core immutable vector implementation is now part of the library's storage story, not just an internal detail, because it supports the flattened and more uniform execution model across packages.
Fields
data- The immutable array containing the vector elements.
Functions and Methods
fn[T] Vector::from_array(arr : Array[T]) -> Vector[T]- Description Creates an immutable vector from a mutable array.
- Parameters
arr:Array[T]- The input mutable array.
- Returns
Vector[T]- An immutable vector containing the elements of the array.
fn[T] Vector::make(n : Int, elem : T) -> Vector[T]- Description Creates a new vector of length
nwith all elements initialized toelem. - Parameters
n:Int- The length of the vector.elem:T- The initial value for all elements.
- Returns
Vector[T]- The newly created vector.
- Description Creates a new vector of length
fn[T] Vector::makei(n : Int, f : (Int) -> T) -> Vector[T]- Description Creates a new vector of length
nwhere each element is generated by applying the functionfto its index. - Parameters
n:Int- The length of the vector.f:(Int) -> T- The generation function.
- Returns
Vector[T]- The generated vector.
- Description Creates a new vector of length
fn[T] length(self : Vector[T]) -> Int- Description Returns the number of elements in the vector.
fn[T] Vector::op_get(self : Vector[T], i : Int) -> T- Description Retrieves the element at the specified index. Supports
v[i]syntax.
- Description Retrieves the element at the specified index. Supports
fn[T] set(self : Vector[T], i : Int, x : T) -> Vector[T]- Description Returns a new vector with the element at index
ireplaced byx. The original vector remains unchanged.
- Description Returns a new vector with the element at index
fn[T, U] map(self : Vector[T], f : (T) -> U) -> Vector[U]- Description Creates a new vector by applying function
fto each element of the vector.
- Description Creates a new vector by applying function
fn[T : Add] add_constant(self : Vector[T], cst : T) -> Vector[T]- Description Adds a constant
cstto each element of the vector.
- Description Adds a constant
fn[T, U, V] zip_with(self : Vector[T], other : Vector[U], f : (T, U) -> V) -> Vector[V]- Description Combines two vectors of the same length by applying binary function
fto corresponding pairs of elements.
- Description Combines two vectors of the same length by applying binary function
fn[T : Add] add(self : Vector[T], other : Vector[T]) -> Vector[T]- Description Element-wise addition of two vectors. Supports
+operator.
- Description Element-wise addition of two vectors. Supports
fn[T : Mul] mul(self : Vector[T], other : Vector[T]) -> Vector[T]- Description Element-wise multiplication (Hadamard product). Supports
*operator.
- Description Element-wise multiplication (Hadamard product). Supports
fn[T : Neg] neg(self : Vector[T]) -> Vector[T]- Description Element-wise negation. Supports
-vsyntax.
- Description Element-wise negation. Supports
fn[T : Mul] left_scale(self : Vector[T], scalar : T) -> Vector[T]- Description Scales the vector by multiplying each element by a scalar from the left.
fn[T : Mul] right_scale(self : Vector[T], scalar : T) -> Vector[T]- Description Scales the vector by multiplying each element by a scalar from the right.
fn[T : One + Mul + Add + Neg] lerp(self : Vector[T], other : Vector[T], alpha : T) -> Vector[T]- Description Performs linear interpolation between two vectors:
(1 - alpha) * self + alpha * other.
- Description Performs linear interpolation between two vectors:
fn[T : Add + Mul] lin_comb(scalar_a : T, self : Vector[T], scalar_b : T, other : Vector[T]) -> Vector[T]- Description Computes the linear combination
scalar_a * self + scalar_b * other.
- Description Computes the linear combination
fn[T] to_col_matrix(self : Vector[T]) -> Matrix[T]- Description Converts the vector to a column matrix (n × 1).
fn[T] to_row_matrix(self : Vector[T]) -> Matrix[T]- Description Converts the vector to a row matrix (1 × n).
fn[T : Zero] scaled_matrix(self : Vector[T]) -> Matrix[T]- Description Creates a diagonal matrix with diagonal elements from the vector.
fn[T : Mul] tensor_product(self : Vector[T], other : Vector[T]) -> Matrix[T]- Description Computes the tensor product (outer product) of two vectors.