Arrays
An Array is a dynamically sized, ordered collection of elements. Backed by contiguous native memory, Autolang arrays are heavily optimized for CPU cache locality.
Creating Arrays
Autolang is statically typed, requiring you to specify the element type. You can initialize arrays using the explicit constructor or via the clean syntactic sugar for inline declarations.
// 1. Explicit Constructor
val numbers = Array<Int>()
numbers.reserve(10) // Pre-allocate memory if capacity is known
numbers.add(10)
numbers.add(20)
// 2. Sugar Syntax
val primes = <Int>[2, 3, 5, 7, 11]
println("Is primes empty? ${if (primes.isEmpty()) "Yes" else "No"}")
println("Size is: ${primes.size()}")Multidimensional Arrays
The sugar syntax extends beautifully to nested arrays, making matrix creation and function arguments much cleaner.
// 2D Array using sugar syntax
val matrix = <Array<Int>>[
[1, 2, 3],
[4, 5, 6]
]
// Implicitly passing arrays as arguments
func processMatrix(data: Array<Array<Int>>) {
// ...
}
processMatrix([[1, 2], [3, 4]])
processMatrix([])Accessing & Modifying
Autolang natively supports the index operator [], which compiles to get() and set() calls under the hood. You can also manipulate elements from specific positions.
val tasks = <String>["Sleep", "Code", "Eat"]
// Access an element
println(tasks[0])
// Update an element
tasks[2] = "Drink Coffee"
// Insert and remove
tasks.insert(1, "Think") // ["Sleep", "Think", "Code", "Drink Coffee"]
println(tasks.toString())
val lastTask = tasks.pop() // Removes and returns "Drink Coffee"
println(tasks.toString())- Arrays in Autolang are zero-indexed.
- Attempting to access an index out of bounds will result in a runtime exception.
Iteration & Functional Methods
You can loop through arrays using the standard for...in syntax, or utilize the built-in functional methods like forEach, filter, and sort by passing named callback functions.
val a = <Int>[5, 9, 12, 6, 3, 4]
// 1. Standard for...in loop
for (num in a) {
// ...
}
// 2. Functional Filter (using a named callback)
func isGreaterThanFive(value: Int): Bool = value > 5
val filtered = a.filter(isGreaterThanFive) // [9, 12, 6]
println(filtered.toString())
// 3. Sorting
func sortAscending(a: Int, b: Int): Int = when {
a > b -> 1
a == b -> 0
else -> -1
}
a.sort(sortAscending) // [3, 4, 5, 6, 9, 12]
println(a.toString())Methods Reference
Modification
add(value: T)- Appends an element to the end.insert(index: Int, value: T)- Inserts an element at a specific index.remove(index: Int)- Removes the element at the specified index.pop(): T?- Removes and returns the last element.clear()- Instantly removes all elements.set(index: Int, value: T)- Updates the element at the index (orarr[index] = value).
Querying
get(index: Int): T- Returns the element at the index (orarr[index]).size(): Int- Returns the total number of elements.isEmpty(): Bool- Returns true if the array has 0 elements.contains(value: T): Bool- Checks if the array contains the value.indexOf(value: T): Int- Returns the first index of the value, or -1 if not found.
Functional & Utilities
forEach(fn: (T) -> Void)- Executes a provided function once for each array element.filter(fn: (T) -> Bool): Array<T>- Creates a new array with elements that pass the test implemented by the provided function.sort(comparator: (T, T) -> Int)- Sorts the elements of the array in place.slice(from: Int, to: Int): Array<T>- Returns a shallow copy of a portion of an array.reserve(capacity: Int)- Pre-allocates memory to optimize performance.toString(): String- Returns a string representation of the array.
