Skip to main content

List Library (List)

Linked to the list type — Collections covers the core operations already; this page is the complete reference.

Query

List.len(array: list) -> int

list-len.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  pln(array.len());
}
Output

List.empty(array: list) -> bool

list-empty.stof
#[main]
fn main() {
  const array = [1];
  pln(array.empty());
}
Output

List.any(array: list) -> bool

list-any.stof
#[main]
fn main() {
  const array = [1];
  pln(array.any());
}
Output

List.contains(array: list, value: unknown) -> bool

list-contains.stof
#[main]
fn main() {
  const array = [1];
  pln(array.contains(1));
}
Output

List.index_of(array: list, v: unknown) -> int

-1 if not found.

list-index-of.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  pln(array.index_of(2));
}
Output

List.is_uniform(array: list) -> str

If every value has the exact same specific type, will return that type — prototype inheritance isn't considered. Otherwise, returns null.

list-is-uniform.stof
#[main]
fn main() {
  const array = ["hi", 'true'];
  pln(array.is_uniform());
}
Output

Access

List.at(array: list, index: int) -> unknown

&array[i] for a reference, so mutating the result mutates the list itself.

list-at.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  let v = &array[1];
  v = 5;
  pln(array);
}
Output

List.front(array: list) -> unknown

list-front.stof
#[main]
fn main() {
  const array = [1, 2];
  pln(array.front());
}
Output

List.back(array: list) -> unknown

list-back.stof
#[main]
fn main() {
  const array = [1, 2];
  pln(array.back());
}
Output

Modify

List.push_back(array: list, ..) -> void

Takes any number of values.

list-push-back.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  array.push_back(4, 5);
  pln(array);
}
Output

List.push_front(array: list, ..) -> void

list-push-front.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  array.push_front(4, 5);
  pln(array);
}
Output

List.pop_back(array: list) -> unknown

list-pop-back.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  pln(array.pop_back(), array);
}
Output

List.pop_front(array: list) -> unknown

list-pop-front.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  pln(array.pop_front(), array);
}
Output

List.insert(array: list, index: int, val: unknown) -> void

list-insert.stof
#[main]
fn main() {
  const array = [2, 1];
  array.insert(1, 3);
  pln(array);
}
Output

List.replace(array: list, index: int, val: unknown) -> unknown

Returns the old value.

list-replace.stof
#[main]
fn main() {
  const array = [2, 1];
  pln(array.replace(1, 4), array);
}
Output

List.append(array: list, other: list) -> void

other is left unmodified.

list-append.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  array.append([4, 5]);
  pln(array);
}
Output

List.clear(array: list) -> void

list-clear.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  array.clear();
  pln(array);
}
Output

List.join(array: list, sep: str) -> str

list-join.stof
#[main]
fn main() {
  const array = ["hello", "world"];
  pln(array.join(", "));
}
Output

Removing

List.remove(array: list, index: int) -> unknown

Returns the removed value, or null if the index was out of bounds.

list-remove.stof
#[main]
fn main() {
  const array = [1];
  pln(array.remove(0), array.empty());
}
Output

List.remove_first(array: list, val: unknown) -> unknown

list-remove-first.stof
#[main]
fn main() {
  const array = [2, 1, 1, 2];
  pln(array.remove_first(2), array);
}
Output

List.remove_last(array: list, val: unknown) -> unknown

list-remove-last.stof
#[main]
fn main() {
  const array = [2, 1, 1, 2];
  pln(array.remove_last(2), array);
}
Output

List.remove_all(array: list, val: unknown) -> bool

Returns whether anything was actually removed.

list-remove-all.stof
#[main]
fn main() {
  const array = [2, 1, 1, 2];
  pln(array.remove_all(2), array);
}
Output

Sort & Reverse

List.sort(array: list) -> void

list-sort.stof
#[main]
fn main() {
  const array = [2, 1, 4, 3];
  array.sort();
  pln(array);
}
Output

List.sort_by(array: list, func: fn) -> void

The comparator returns negative/zero/positive, same convention as everywhere else. Written as a single ternary expression here rather than a chain of bare if statements — if can't produce a value on its own (see Control Flow), so a chain of them with no else wouldn't actually control the sort order the way it looks like it would.

list-sort-by.stof
#[main]
fn main() {
  const array = [2, 1, 4, 3];
  array.sort_by((a: int, b: int): int => a < b ? 1 : (a > b ? -1 : 0));
  pln(array);
}
Output

List.reverse(array: list) -> void

In place.

list-reverse.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  array.reverse();
  pln(array);
}
Output

List.reversed(array: list) -> list

Returns a new reversed list, leaving the original untouched.

list-reversed.stof
#[main]
fn main() {
  const array = [1, 2, 3];
  const other = array.reversed();
  pln(array, other);
}
Output

Convert

List.to_uniform(array: list, type: str) -> void

Casts every value to the given type in place — throws if any value can't be cast.

list-to-uniform.stof
#[main]
fn main() {
  const array = [1, "hi", true];
  array.to_uniform("str");
  pln(array);
}
Output