Skip to content

List

fn map[A, B](xs: List[A], f: fn(A) -> B fails E(f) uses { R(f) }) -> List[B] fails E(f) uses { R(f) }

Applies f to each element.

fn each[A](xs: List[A], f: fn(A) -> () fails E(f) uses { R(f) }) -> () fails E(f) uses { R(f) }

Calls f on each element, from the first to the last. Stops at the first failure.

fn filter[A](xs: List[A], f: fn(A) -> Bool fails E(f) uses { R(f) }) -> List[A] fails E(f) uses { R(f) }

Keeps the order.

fn fold[A, B](xs: List[A], from: B, f: fn(B, A) -> B fails E(f) uses { R(f) }) -> B fails E(f) uses { R(f) }

Folds from the front: f(f(f(from, x0), x1), x2).

fn length[A](xs: List[A]) -> Int

The number of elements.

fn is_empty[A](xs: List[A]) -> Bool

Whether the list is empty.

fn get[A](xs: List[A], index: Int) -> Option[A]

Zero-based. None if out of range or negative.

fn head[A](xs: List[A]) -> Option[A]

None if empty.

fn tail[A](xs: List[A]) -> List[A]

Empty if empty.

fn reverse[A](xs: List[A]) -> List[A]

The elements in reverse order.

fn append[A](xs: List[A], ys: List[A]) -> List[A]

Joins two lists: the elements of xs, then those of ys.

fn join(xs: List[String], sep: String) -> String

Joins the strings with sep in between: ["a", "b"].join(", ") is "a, b". s.split(sep).join(sep) == s holds.

fn flatten[A](xss: List[List[A]]) -> List[A]

Flattens a list of lists, keeping the order.

fn partition[A](xs: List[A], f: fn(A) -> Bool fails E(f) uses { R(f) }) -> (List[A], List[A]) fails E(f) uses { R(f) }

Splits into the elements where f is True and those where it is False, both in order. (evens, odds) = xs.partition(is_even).

fn any[A](xs: List[A], f: fn(A) -> Bool fails E(f) uses { R(f) }) -> Bool fails E(f) uses { R(f) }

Stops at the first match. False if empty.

fn all[A](xs: List[A], f: fn(A) -> Bool fails E(f) uses { R(f) }) -> Bool fails E(f) uses { R(f) }

Stops at the first False. True if empty.

fn find[A](xs: List[A], f: fn(A) -> Bool fails E(f) uses { R(f) }) -> Option[A] fails E(f) uses { R(f) }

The first element where f is True.

fn range(from: Int, to: Int) -> List[Int]

The integers from from up to but excluding to, ascending. Empty if to <= from.

fn zip[A, B](xs: List[A], ys: List[B]) -> List[(A, B)]

Pairs elements at the same position. As long as the shorter list.

fn take[A](xs: List[A], n: Int) -> List[A]

The first n elements, or all of them if there are fewer. Empty if n is not positive.

fn drop[A](xs: List[A], n: Int) -> List[A]

The elements after the first n. All of them if n is not positive.

fn last[A](xs: List[A]) -> Option[A]

The last element.

fn flat_map[A, B](xs: List[A], f: fn(A) -> List[B] fails E(f) uses { R(f) }) -> List[B] fails E(f) uses { R(f) }

f on each element, with the lists it returns joined in order.

fn filter_map[A, B](xs: List[A], f: fn(A) -> Option[B] fails E(f) uses { R(f) }) -> List[B] fails E(f) uses { R(f) }

The values of f that are Some, in order.

fn find_index[A](xs: List[A], f: fn(A) -> Bool fails E(f) uses { R(f) }) -> Option[Int] fails E(f) uses { R(f) }

The index of the first element for which f is True.

fn enumerate[A](xs: List[A]) -> List[(Int, A)]

Each element paired with its index, from 0.

fn unzip[A, B](xys: List[(A, B)]) -> (List[A], List[B])

The first and second of each pair, as two lists.

fn sort_by[A](xs: List[A], f: fn(A, A) -> Ordering fails E(f) uses { R(f) }) -> List[A] fails E(f) uses { R(f) }

Sorted by f, a merge sort. Stable: elements that f finds Equal keep their order.

fn sort[A: Ord](xs: List[A]) -> List[A]

Sorted in the order of Ord ([trait.builtin]). Stable.