List
Lists are Ard’s growable ordered collections. A list type is written [T], where T is the element type.
let numbers: [Int] = [1, 2, 3]let names: [Str] = ["Ada", "Grace"]Methods
Section titled “Methods”fn size() Int
Section titled “fn size() Int”Return the number of elements.
let count = [1, 2, 3].size() // 3fn at(index: Int) T?
Section titled “fn at(index: Int) T?”Return the element at index, or Maybe::new<T>() if the index is out of bounds.
let values = [10, 20]let first = values.at(0).or(0) // 10let missing = values.at(9) // Int?fn slice(start: Int?, end: Int?) Slice<T>?
Section titled “fn slice(start: Int?, end: Int?) Slice<T>?”Return a fixed-length shared view over a checked, half-open range. Both bounds are optional:
let values = [10, 20, 30, 40]let tail = values.slice(start: 1) // Slice<Int>?let head = values.slice(end: 3) // Slice<Int>?let middle = values.slice(start: 1, end: 3) // Slice<Int>?Invalid bounds return none. See Slice<T> for aliasing, mutation, copying, and Go interop semantics.
fn push(value: T) Int
Section titled “fn push(value: T) Int”Append value through a list reference and return the new length.
let values = mut [1]let size = values.push(42)fn prepend(value: T) Int
Section titled “fn prepend(value: T) Int”Insert value at the beginning through a list reference and return the new length.
let values = mut [2, 3]values.prepend(1)fn set(index: Int, value: T) Bool
Section titled “fn set(index: Int, value: T) Bool”Replace the element at index through a list reference. Returns true if the index existed, or false if it was out of bounds.
let values = mut [1, 2, 3]let updated = values.set(1, 20)fn swap(l: Int, r: Int)
Section titled “fn swap(l: Int, r: Int)”Swap two elements through a list reference.
let values = mut [1, 2, 3]values.swap(0, 2)fn sort(cmp: fn(T, T) Bool)
Section titled “fn sort(cmp: fn(T, T) Bool)”Sort through a list reference using a comparison callback. The callback returns true when the first argument should come before the second.
let values = mut [3, 1, 2]values.sort(fn(a: Int, b: Int) Bool { a < b })Module helpers
Section titled “Module helpers”The ard/list module provides helper functions such as list::map, list::keep, and list::find. Import that module when you want those helpers; list methods are available without an import.