#root::data

List root/data/List.vi

struct List[T](...);

A linear collection of T.

Lists are optimized for fast concatenation and in-order iteration. Accessing the first value in a list is O(1), but accessing an arbitrary value is O(n).

let list = [1, 2, 3];
list.push_front(0);
list // [0, 1, 2, 3]
list.push_back(4);
list // [0, 1, 2, 3, 4]
list ++= [5, 6];
list // [0, 1, 2, 3, 4, 5, 6]
list.pop_front() // Some(0)
list // [1, 2, 3, 4, 5, 6]
for value in list.iter() {
  io.println("{value}"); // prints 1, 2, 3, 4, 5, 6
}

If frequently accessing elements by index, consider using an array instead.

empty root/data/List.vi:32

const empty[T]: List[T];

len root/data/List.vi:35

fn len[T](self: &List[T]) -> N32;

The length of the list. O(1)

new root/data/List.vi:44

fn new[T; Fork[T], Drop[T]](len: N32, value: T) -> List[T];

Create a list with len copies of value.

let list = List::new(3, ".");
list // [".", ".", "."]

from_fn root/data/List.vi:59

fn from_fn[T, F; Fork[F], Drop[F], Fn[F, (), T]](len: N32, f: F) -> List[T];

Create a list with the given length, where each value is produced in order by calling the function. O(n)

let i = 0;
let list = List::from_fn(3, fn* () { i += 1; i });
list // [1, 2, 3]

at root/data/List.vi:75

fn at[T](...: &List[T], i: N32) -> Option[&T];

Access the ith element of the list. O(i)

let list = ["a", "b", "C"];
list.at(1) // Some(&"b")
list.at(2).assume().* = "c";
list // List(["a", "b", "c"])

If this is being done regularly, consider using an Array instead.

get root/data/List.vi:96

fn get[T; Fork[T]](self: &List[T], i: N32) -> Option[T];

Get the ith element of the list. O(i)

let array = ["a", "b", "c"];
array.get(0) // Some("a")
array.get(1) // Some("b")
array.get(3) // None

If this is being done regularly, consider using an Array instead.

slice root/data/List.vi:100

fn slice[T, B1, B2; Drop[T], Drop[B1], Drop[B2], Bound[B1, N32], Bound[B2, N32]](self: List[T], ...: Range[B1, B2]) -> List[T];

split_at root/data/List.vi:122

fn split_at[T](self: List[T], i: N32) -> (List[T], List[T]);

map root/data/List.vi:134

fn map[T, U, F; Fork[F], Drop[F], Fn[F, (T,), U]](...: List[T], f: F) -> List[U];

filter root/data/List.vi:150

fn filter[T, F; Drop[T], Fork[F], Drop[F], Fn[F, (&T,), Bool]](self: List[T], pred: F) -> List[T];

pop_front root/data/List.vi:160

fn pop_front[T](...: &List[T]) -> Option[T];

head_tail root/data/List.vi:171

fn head_tail[T](...: List[T]) -> Option[(T, List[T])];

concat root/data/List.vi:181

impl concat[T]: Concat[List[T], List[T], List[T]];

push_back root/data/List.vi:196

fn push_back[T](list: &List[T], el: T);

Append a value to the end of the list. O(1)

let list = [1, 2, 3];
list.push_back(4);
list // [1, 2, 3, 4]

push_front root/data/List.vi:206

fn push_front[T](list: &List[T], el: T);

Append a value to the beginning of the list. O(1)

let list = [1, 2, 3];
list.push_front(0);
list // [0, 1, 2, 3]

insert root/data/List.vi:218

fn insert[T](list: &List[T], i: N32, el: T);

Insert an element at position i in the list.

let list = ["foo", "bar", "qux"]
list.insert(2, "bar");
list // ["foo", "bar", "baz", "qux"]

If i is greater than the length of the list, the element will be added to the end.

find root/data/List.vi:229

fn find[T, F; Fork[F], Drop[F], Fn[F, (&T,), Bool]](self: &List[T], pred: F) -> Option[&T];

contains root/data/List.vi:239

fn contains[T; Eq[T]](self: &List[T], el: &T) -> Bool;

join root/data/List.vi:243

fn join[T; Cast[T, String]](list: List[T], sep: String) -> String;

drop_while root/data/List.vi:261

fn drop_while[T, F; Drop[T], Fork[F], Drop[F], Fn[F, (&T,), Bool]](...: &List[T], pred: F);

Remove items from the beginning of the list as long as the predicate returns true.

let list = [0, 1, 3, 5, 0, 6, 0, 2];
list.drop_while(fn* (value: N32) { value <= 3 });
list // [5, 0, 6, 0, 2]

trim_back_where root/data/List.vi:280

fn trim_back_where[T, F; Drop[T], Fork[F], Drop[F], Fn[F, (&T,), Bool]](self: &List[T], pred: F);

Remove items from the beginning of the list as long as the predicate returns true.

let list = [0, 1, 3, 5, 0, 6, 0, 2];
list.trim_back_where(fn* (value: N32) { value <= 3 });
list // [0, 1, 3, 5, 0, 6]

show root/data/List.vi:295

impl show[T; Show[T]]: Show[List[T]];

sort root/data/List.vi:311

fn sort[T; Ord[T]](list: &List[T]);

Stably sort the list in ascending order. O(n log n)

let list = [3, 1, 6, 4, 7];
list.sort();
list // [1, 3, 4, 6, 7]

is_sorted root/data/List.vi:364

fn is_sorted[T; Ord[T]](list: &List[T]) -> Bool;

Checks whether the list is sorted in ascending order.

[3, 1, 6, 4, 7].is_sorted() // false
[1, 3, 4, 6, 7].is_sorted() // true

iter root/data/List.vi:391

fn iter[T](self: List[T]) -> Iter[T];

Iterate over the elements of the list.

let list = [1, 0, 4, 6];
for value in list.iter() {
  io.println("{list}");
}
1
0
4
6

Iter root/data/List.vi:395

struct Iter[T](...);

iter_ref root/data/List.vi:426

fn iter_ref[T](...: &List[T]) -> IterRef[T];

Iterate over the elements of the list by reference.

let list = [0, 5, 3, 5];
for &value in list.iter_ref() {
  value += 1;
}
list // [1. 6. 4. 6]

IterRef root/data/List.vi:430

struct IterRef[T](...);

collect root/data/List.vi:461

impl collect[T]: Collect[List[T], T];

Collect the items of an iterator into a list.

(0..6).collect[List, _, _]() // [0, 1, 2, 3, 4, 5]

reversed root/data/List.vi:476

fn reversed[T](self: List[T]) -> List[T];

Construct a list with the elements reversed.

let list = [1, 2, 3, 4];
list.reversed() // [4, 3, 2, 1]

reverse root/data/List.vi:490

fn reverse[T](self: &List[T]);

Reverse the elements of the list.

let list = [1, 2, 3, 4];
list.reverse();
list // [4, 3, 2, 1]

eq root/data/List.vi:494

impl eq[T; Eq[T]]: Eq[List[T]];

ord root/data/List.vi:517

impl ord[T; Ord[T]]: Ord[List[T]];

fork root/data/List.vi:551

impl fork[T; Fork[T]]: Fork[List[T]];

drop root/data/List.vi:562

impl drop[T; Drop[T]]: Drop[List[T]];
  1. Iter

    1. IterRef