Vector operations
All four functions receive vectors by value in Blyx source. The interpreter shares immutable element storage across calls. An index is an i64; an element, sum or dot product is an f64.
| Call | Result | Example |
|---|---|---|
| len(vector) | i64 count, zero for an empty vector | len([4.0, 9.0]) → 2 |
| at(vector, i64) | The element at a zero-based index | at([4.0, 9.0], 1) → 9 |
| sum(vector) | Sum of the elements | sum([4.0, 9.0]) → 13 |
| dot(vector, vector) | Sum of products at matching indices | dot([1.0, 2.0], [4.0, 9.0]) → 22 |
Preconditions
at fails on a negative index, on index len(v), and on any access to an empty vector. dot fails if lengths differ, even when one side is empty. The sum of an empty vector and the dot product of two empty vectors are numerically zero. Check lengths before indexing or computing an average.
Output macros
print!() emits no text. println!() emits a newline. With a single expression, print!(value) displays its value, and println!(value) follows it with a newline. Use consecutive calls to combine text and numbers. Braces inside an output string are literal characters.
Cost of the operations
Reading the length or one element requires no scan. sum and dot visit the elements and their work grows with vector length. The interpreter charges its execution budget for these reductions. Holding the sum in a binding avoids recomputing it in every loop iteration.
Practice
Calculate dot([2.0, -1.0], [3.0, 4.0]) by hand. What happens if you remove the 4.0 from the second vector?
Hint
Multiply the corresponding entries before summing.
Worked solution
2 × 3 + (−1 × 4) is 2. After removing an entry the lengths differ, and the runtime reports an error.