module Arrays {
// ARRAY BASICS
// arrays can hold different types
// arrays are immutable by default
// see mutability section for more info on mutable arrays
fn test() {
let x1 = [true, 1, false]; // type is array of bool | int
// arrays have functions that return new arrays based on an update
let x3 = x1.append([1, 2, 3]);
// the number of elements in array is accessed using #
let x5 = x1#; // 3
// individual elements are accessed using [], zero based
let x7 = x1[1]; // 1
// array accesses that are out of range will throw
let x9 = x1[3]; // throws
// within an expression in [], the number of elements of the array being accessed
// can be accessed using #. for example, to get the last element, use # - 1
let x11 = x1[# - 1];
// arrays allow iteration
for (x in x2) {
print(x); // prints values of x2
}
// array iteration can include the index by providing a second variable
for (x, index in x2) {
print(index, x); // prints index and values of x2
}
// arrays support the "spread" operator (...), which will flatten an iterable
let x13 = [...x1, false, 1];
// subranges of arrays (slices) can be obtained (slices are just arrays)
let x14 = [1, 2, 3, 4, 5];
let x15 = x4.slice(1, 4); // [2, 3, 4]
// use v ## n to repeat a value v for n times when constructing an array
let x16 = ["a" ## 2, "b" ## 3]; // ["a", "a", "b", "b", "b"]
}
}