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. 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
fun 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. Arrays are zero-indexed. Accessing an out-of-bounds index results in a runtime exception.
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("Initial: $tasks")
val lastTask = tasks.pop() // Removes and returns "Drink Coffee"
println("Final: $tasks")Iteration & Functional Methods
Loop through arrays using for...in, or use built-in functional methods like forEach, filter, and sort with closures.
val a = <Int>[5, 9, 12, 6, 3, 4]
// 1. Standard for...in loop
for (num in a) {
// ...
}
// 2. Iteration using forEach and a closure
a.forEach {|num| println("Number: " + num) }
// 2b. Iteration with index
a.forEach {|num, idx| println("Number at " + idx + ": " + num) }
// 3. Functional Filter
val filtered = a.filter {|value| value > 5 } // [9, 12, 6]
println("Filtered: $filtered")
// 4. Sorting
a.sort {|a, b| -> {
return when {
a > b -> 1
a == b -> 0
else -> -1
}
}}
println("Sorted: $a")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)/forEach(fn: (T, Int) -> Void)— Executes a closure once for each array element (optionally receiving the index as the second argument).filter(fn: (T) -> Bool): Array<T>— Creates a new array with elements that pass the test.sort(comparator: (T, T) -> Int)— Sorts elements in place using a comparator closure.slice(from: Int, to: Int): Array<T>— Returns a shallow copy of a portion of the array.reserve(capacity: Int)— Pre-allocates memory to optimize performance.toString(): String— Returns a string representation of the array.
