Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

List

Lists are mutable in Andy C++.

let my_list = [1,2,3];

// Values inside a list can be changed
my_list[2] = 4;
print(my_list);           // [1,2,4]

// You can add elements to the end of a list
my_list.push(99);
print(my_list);           // [1,2,4,99]

// Remove and return the last element of the list
let element = my_list.pop();
print(element, my_list);  // 99 [1,2,4]

The values of a list share one type. To put values of different types in one list, like [1, "a"], annotate it: let xs: [Any] = [1, "a"]; (see Values of different types).

Indexing

Lists, strings, and tuples also support negative indexes. Negative indexes count from the end.

let my_list = [1,2,3,4,5,6,7,8,9];
print(my_list[-1]);  // 9

Element accessors

Besides the [] operator, lists provide four element accessor functions. They differ on two axes: whether negative indexes are allowed, and what happens when the index is out of bounds.

functionindexout of bounds
getnon-negative only (negative is an error)error
get?non-negative only (negative is an error)None
indexsigned; negatives wrap from the enderror
index?signed; negatives wrap from the endNone
let my_list = [10, 20, 30];

print(my_list.get(0));      // 10
print(my_list.get?(9));     // None
print(my_list.index(-1));   // 30
print(my_list.index?(-9));  // None

index works like the [] operator, and index? gives None instead of an error.

First and last

first and last give the first and last element of a list (or any sequence), and with a function, the first and last element the function gives true for. Their ? forms give None where they would fail:

let numbers = [1, 4, 5, 8, 9];
print(numbers.first, numbers.last);                // 1 9
print(numbers.first(fn(x) => x % 2 == 0));         // 4
print(numbers.last(fn(x) => x % 2 == 0));          // 8
print(numbers.first?(fn(x) => x > 10), [].last?);  // None None

Runs

chunk_by splits a list (or any sequence) into runs of equal neighbours, and with a function, into runs of neighbours the function gives true for:

print([1, 1, 2, 1, 1].chunk_by());                         // [[1,1],[2],[1,1]]
print([1, 2, 3, 5, 6].chunk_by(fn(a, b) => b == a + 1));  // [[1,2,3],[5,6]]

To group equal elements wherever they are, rather than only neighbours, use group_by on maps.

Slicing

Use ranges to slice lists. Ranges can be inclusive or exclusive. Negative indices count from the end of the list.

let my_list = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100];

// Exclusive range: 3 to 6 (does not include index 6)
print(my_list[3..6]);    // [30,40,50]

// Inclusive range: 3 to 6 (includes index 6)
print(my_list[3..=6]);   // [30,40,50,60]

// Negative indices: Counting from the end of the list
print(my_list[-3..-1]);  // [80,90]

A range whose start lands after its end (for example my_list[6..3]) is out of bounds and raises an error rather than returning a reversed or empty slice. This holds for lists, strings, tuples and deques alike.

Operators

OperatorFunction
++Concatenation
<>Coerce operands into strings and concatenate
inChecks if an element is present in the list
not inChecks if an element is not present in the list
==Equality
!=Inequality
>Greater (lexicographically)
<Less (lexicographically)
>=Greater equals (lexicographically)
<=Less equals (lexicographically)