immut/vector Tutorial
Small Case: Rebuild A Published Feature Vector
moonbit
///|
fn rebuild_release_vector(
base : @immut.Vector[Int],
manual_override : Int,
) -> @immut.Matrix[Int] {
let corrected = base.set(1, manual_override)
let expanded = @immut.lin_comb(
2,
corrected,
1,
@immut.Vector::from_array([1, 0, 1]),
)
expanded.tensor_product(@immut.Vector::from_array([1, 0]))
}
///|
test "immut vector tutorial case" {
let base = @immut.Vector::from_array([2, 4, 6])
let result = rebuild_release_vector(base, 9)
inspect(base, content="|2, 4, 6|")
inspect(result, content="|5, 0|\n|18, 0|\n|13, 0|")
}This case reads like a tiny release-preparation pipeline:
- Start from a published feature vector.
- Apply one explicit correction with
set. - Build a new weighted vector with
lin_comb. - Expand it into a matrix-shaped artifact with
tensor_product.
At no point is the original vector mutated, so earlier states remain usable.
Suggested Flow
- Create vectors with
Vector::from_array,Vector::make, orVector::makei. - Use
set,map,left_scale, andright_scalewhen you want a new vector. - Use
tensor_product,scaled_matrix,to_row_matrix, andto_col_matrixfor matrix-facing conversions.
Practical Guidance
- Choose
@immut.Vectorwhen downstream code benefits from explicit value semantics. - Use
@mutable.Vectorinstead when repeated in-place updates or a publicdot()helper are part of the workload. - Use the immutable path when every structural change should produce a fresh value that can be passed onward safely.