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 push(value: T) Int
Section titled “fn push(value: T) Int”Append value to a mutable list and return the new length.
mut values: [Int] = []let size = values.push(42)fn prepend(value: T) Int
Section titled “fn prepend(value: T) Int”Insert value at the beginning of a mutable list and return the new length.
mut values = [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 in a mutable list. Returns true if the index existed, or false if it was out of bounds.
mut values = [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 in a mutable list.
mut values = [1, 2, 3]values.swap(0, 2)fn sort(cmp: fn(T, T) Bool)
Section titled “fn sort(cmp: fn(T, T) Bool)”Sort a mutable list using a comparison callback. The callback returns true when the first argument should come before the second.
mut values = [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.