Lists
List literals, indexing, length, concatenation, appending, method syntax, and aliasing.
Contents
A list is a homogeneous, ordered sequence with type List[T]. Lists are managed values.
Creating a List#
const numbers = [1, 2, 3]
const names = ["Ada", "Grace"]
const nested = [[1, 2], [3, 4]]
An empty list needs a contextual type from a declaration annotation or function return type.
var values List[i32] = []
const empty_names List[str] = []
fn none() List[i32] {
return []
}
There is no List::new, no List[T]() constructor, and no push. A list is created from a literal and grown with +=.
fn map[T, U](items List[T], transform fn(T) U) List[U] {
var result List[U] = []
for item in items {
result += [transform(item)]
}
return result
}
Element Type Inference#
Numeric elements unify to a common numeric type. An i32 and an i64 in the same literal produce List[i64].
const integers = [1, 2, 3] // List[i32]
const wide = [1, 2_000_000_000] // List[i64]
const reals = [1.0, 2, 3] // List[f64]
A list of records, sums, or other managed values stores references. A list of scalars stores the values directly. Extern records are not allowed inside lists.
Length and Indexing#
len returns the number of elements as i32. Indexing is zero-based and bounds-checked at runtime.
const values = [10, 20, 30]
print(values.len) // 3
print(values[0]) // 10
print(values[2]) // 30
Indexing is an lvalue when the binding is mutable.
var values = [10, 20, 30]
values[1] = 99
print(values[1]) // 99
get returns an Option instead of failing.
const values = [10, 20, 30]
if values.get(5) is Some(value) {
print(value)
} else {
print("out of range")
}
Concatenation and Append#
+ concatenates two lists with the same element type. += appends to a var binding and rebinds it.
const first = [1, 2]
const second = [3, 4]
const all = first + second
var built = [0]
for i in 1..4 {
built += [i]
}
Appending another list is allowed. values += values duplicates the current contents.
var values = [1, 2, 3]
values += values
print(values.len) // 6
+ produces a new list. += rebinds the variable. Element assignment mutates the existing list.
var left = [1, 2]
var right = left
right[0] = 9
print(left[0]) // 9
right = [7, 8]
print(left[0]) // 9
Iteration#
for item in list visits elements in order. The loop variable is a copy for scalar elements and a reference for managed elements. The loop captures the list length when it starts.
fn sum(values List[i32]) i32 {
var total = 0
for value in values {
total += value
}
return total
}
Use a range loop when the algorithm needs the index or neighbouring elements.
fn adjacent_pairs(values List[i32]) List[i32] {
var out List[i32] = []
for i in 0..values.len - 1 {
out += [values[i] + values[i + 1]]
}
return out
}
Method Syntax#
A free function whose first parameter is a list can be called with method syntax.
const result = [1, 2, 3, 4, 5]
.map(fn(x) => x * 2)
.filter(fn(x) => x > 4)
values.map(f) means map(values, f).
Operations#
| Operation | Syntax | Result |
|---|---|---|
| Length | xs.len |
i32 |
| Index | xs[i] |
T |
| Checked index | get(xs, i) or xs.get(i) |
Option[T] |
| Element assignment | xs[i] = value |
Updates the list |
| Concatenation | xs + ys |
List[T] |
| Append and rebind | xs += [value] |
List[T] |
| Element iteration | for x in xs |
Visits each T |
| Index iteration | for i in 0..xs.len |
Visits each index |
Contracts#
A postcondition may describe a list's length, ordering, or relationship to the input.
pure fn sorted(values List[i32]) bool {
for i in 0..values.len - 1 {
if values[i] > values[i + 1] {
return false
}
}
return true
}
fn sort(values List[i32]) List[i32]
: post r => sorted(r)
: post r => r.len == values.len
{
// implementation
return values
}
The loop in sorted is safe for an empty list because 0..-1 performs no iterations. A helper called from a postcondition must be pure.
Performance and Aliasing#
Lists are heap-allocated and grow with a capacity at least equal to their length. Capacity is not exposed. Build a list by appending when the size is unknown, and use a literal when the contents are fixed. Avoid sharing a mutable list between components unless mutation is part of the documented interface.