Velo by Example: Arrays

Arrays in Velo are typed collections. They are declared with array[T] syntax.

Create arrays with initial values or a fixed size.

array[int] numbers = new array[int]{1, 2, 3, 4, 5};
array[int] empty = new array[int](10);

Access elements by index. Zero-based indexing.

array[int] arr = new array[int]{10, 20, 30};
int first = arr[0];     # 10
int second = arr[1];    # 20
arr[2] = 40;            # modify element

The len property returns the array length.

array[int] arr = new array[int]{1, 2, 3};
int length = arr.len;    # 3

sub(start, end) returns a subarray. con(other) concatenates two arrays. plus(element) adds an element.

array[int] arr = new array[int]{1, 2, 3, 4, 5};
array[int] sub = arr.sub(1, 4);   # [2, 3, 4]

array[int] a = new array[int]{1, 2};
array[int] b = new array[int]{3, 4};
array[int] combined = a.con(b);    # [1, 2, 3, 4]

array[int] extended = a.plus(3);   # [1, 2, 3]

map transforms each element using a callback function.

array[int] numbers = new array[int]{1, 2, 3, 4, 5};
array[int] doubled = numbers.map(
    func(int index, int value) int {
        value * 2
    }
);  # [2, 4, 6, 8, 10]

Multidimensional arrays use nested types.

array[array[int]] matrix = new array[array[int]]{
    new array[int]{1, 2, 3},
    new array[int]{4, 5, 6}
};