xur_

> built-in type

list

methods of list values

in the browser

Lists in Xur are shared and change in place, like Python's: let ys = xs; ys.push(1); changes xs too. These are methods on any list value; there is no module to import.

In the signatures, T is the type of the list's items and U is another type you choose, such as what a closure you pass returns.

Methods that change the list (push, pop, insert, remove, clear, reverse, extend) change it in place. Every other method leaves the list as it is and returns a new value; in particular sort and sort_by return a sorted copy.

Closures passed to filter, find, find_index, any and all must return a bool; any other value raises a runtime error.

> methods

# list.all(f: fn(T) -> bool) -> bool

Whether f returns true for every item. It stops at the first false and is true for an empty list.

# list.any(f: fn(T) -> bool) -> bool

Whether f returns true for at least one item. It stops at the first true and is false for an empty list.

# list.clear()

Removes every item.

# list.contains(item: T) -> bool

Whether an item equal to item is in the list.

# list.dedup() -> list<T>

A copy without consecutive repeated items: [1, 1, 2, 1].dedup() is [1, 2, 1]. Sort first to remove every duplicate.

# list.enumerate() -> list<(i64, T)>

A list with one tuple per item: its index, then the item, so for (i, x) in xs.enumerate() walks indices and items together. [3, 1].enumerate() is [(0, 3), (1, 1)].

# list.extend(other: list)

Appends every item of other. xs.extend(xs) doubles the list.

# list.filter(f: fn(T) -> bool) -> list<T>

A new list of the items for which f returns true.

# list.find(f: fn(T) -> bool) -> Option<T>

The first item for which f returns true, or None.

# list.find_index(f: fn(T) -> bool) -> Option<i64>

The index of the first item for which f returns true, or None.

# list.first() -> Option<T>

The first item, or None for an empty list.

# list.flat_map(f: fn(T) -> list<U>) -> list<U>

Calls f on each item and joins the lists it returns into one list. A result that isn't a list is added as a single item.

# list.flatten() -> list<U>

For a list of lists, one list of all their items. Only one level is removed, and items that aren't lists are kept as they are: [1, [2, 3], [[4]]].flatten() is [1, 2, 3, [4]].

# list.get(index: i64) -> Option<T>

The item at index, or None when the index is negative or past the end.

# list.insert(index: i64, item: T)

Inserts item before the item at index; an index equal to the length appends. Any other index raises a runtime error.

# list.is_empty() -> bool

Whether the list has no items.

# list.join(separator: str) -> str

The items, each formatted as {} would show it, joined with separator.

# list.last() -> Option<T>

The last item, or None for an empty list.

# list.len() -> i64

The number of items.

# list.map(f: fn(T) -> U) -> list<U>

A new list of f applied to each item.

# list.max() -> Option<T>

The smallest or largest item, or None for an empty list. Items are compared as for sort; items that can't be compared raise a runtime error. Of equal items the first is returned.

# list.min() -> Option<T>

The smallest or largest item, or None for an empty list. Items are compared as for sort; items that can't be compared raise a runtime error. Of equal items the first is returned.

# list.pop() -> Option<T>

Removes and returns the last item, or None for an empty list.

# list.push(item: T)

Appends item to the end.

# list.reduce(f: fn(T, T) -> T) -> T

Combines the items from left to right: the first item, then f of that and the second, and so on. A list of one item returns that item; an empty list raises a runtime error.

# list.remove(index: i64) -> T

Removes and returns the item at index. An index that is negative or not less than the length raises a runtime error.

# list.reverse()

Reverses the order of the items in place.

# list.slice(start: i64, end: i64) -> list<T>

A new list of the items from start up to, not including, end. Negative indices count from the end (-1 is the position before the last item) and indices past either end are clamped, so [1, 2, 3].slice(-2, 10) is [2, 3]. When start is not before end, the result is empty.

# list.sort() -> list<T>

A sorted copy, smallest first. The items must be all integers, all floats or all strings (compared by bytes); anything else, including a float NaN, raises a runtime error.

# list.sort_by(compare: fn(T, T) -> i64) -> list<T>

A sorted copy, ordered by compare, which returns a negative integer when its first argument comes first, 0 when they are equal and a positive integer otherwise: xs.sort_by(|a, b| b - a) sorts largest first. Equal items keep their order. A result that isn't an integer raises a runtime error.

# list.sum() -> int | float

The sum of a list of all integers or all floats. An empty list, a mix of integers and floats, other items, or an integer sum that overflows raises a runtime error.

# list.to_hex() -> Result<str, str>

For a list of byte values, lowercase hex with two digits per item: [1, 255].to_hex() is Ok("01ff"). An item that isn't an integer 0–255 gives an Err with a message.

# list.zip(other: list) -> list<(T, U)>

A list of tuples pairing each item with the item at the same index of other, like enumerate. It stops at the end of the shorter list.

> examples

use std::io;

fn main() {
    let xs = [3, 1, 2];
    xs.push(4);
    io::println("len: {}", xs.len());
    io::println("sorted: {}", xs.sort());
    io::println("evens: {}", xs.filter(|x| x % 2 == 0));
    io::println("joined: {}", xs.join(", "));
}