All examples
Language

Fixed and dynamic typed arrays

Declare an element type, compare fixed length with doubling capacity, and see find switch from linear to binary search after sorting.

Download .aner source
arrays_basic.aner
// Both typed array families are built in; no import is required.
let fixed = Array<Int64>(size: 4, fill: 0)
fixed.set(0, 30)
fixed.set(1, 10)
fixed.set(2, 20)
print(fixed.to_list())
fixed.sort()
print(fixed.to_list())
print(fixed.find(20))

let values = DynamicArray<Int64>(capacity: 2)
values.append(30)
values.append(10)
values.append(20) // Capacity doubles from 2 to 4.
print(values.len())
print(values.capacity())
print(values.is_sorted())
print(values.find(20))
values.sort()
print(values.is_sorted())
print(values.find(20))
values.set(0, 99) // Sorted metadata is maintained after every mutation.
print(values.is_sorted())
print(values.find(20))
let independent = values.copy()
values.set(0, 5)
print(independent.to_list())
let words = DynamicArray<String>(capacity: 2)
words.append("pear")
words.append("apple")
words.sort()
print(words.to_list())

Make it your experiment.

With Aner installed, save this program as examples/arrays_basic.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/arrays_basic.aner
aner run examples/arrays_basic.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Use the compatible native Aner interpreter and VS Code extension 0.1.26 or newer. Arrays are built in and require no import. Every array requires an explicit Int64, Float64, Bool, or String element type; see the typed array guide for method contracts and complexity.

Iris and Wine are teaching datasets with their own attribution. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner