The list module

The list module is a collection of functions that operate on lists. It is also the metatable for lists.

Free functions

new(capacity: number): list

Creates a new list with the given capacity. If the capacity is not given, it defaults to an implementation-defined value.

Member functions

append(self, value: any): null

Appends the given value to the list.

clear(self): null

Removes all elements from the list.

clone(self): list

Creates a new list with the same elements as the given list.

extend(self, other: iterable): null

Appends the elements of the given iterable to the list.

flatten(self): list

Returns a new list containing the elements of the given list and all sub-iterables.

join(self, sep: string): string

Returns a string containing the elements of the list separated by the given separator.

pop(self): any

Removes and returns the last element of the list.

remove(self, value: any): null

Removes the first occurrence of the given value from the list.

removeAt(self, index: number): any

Removes the element at the given index from the list and returns it.

reversed(self): list

Returns a new list with the elements of the given list in reverse order.

size(self): number

Returns the number of elements in the list.

slice(self, start: number, end: number): list

Returns a new list containing the elements from start to end (exclusive).

sort(self, cmp: callable[any, any → int] = cmp): null

Sorts the list in-place using the given comparison function. If no comparison function is given, the default comparison function is used. The comparison function should return a negative number if the first argument is less than the second, a positive number if the first argument is greater than the second, and zero if the arguments are equal.

sorted(self, cmp: callable[any, any → int] = cmp): list

Returns a new list containing the elements of the given list sorted using the given comparison function. The comparison function is the same as for sort.

Metamethods

__contains__(self, value: any): boolean

Returns whether the given value is in the list.

__index__(self, key: number): any

Returns the element at the given index.

__iter__(self): any

Returns an iterator over the elements of the list.

__plus__(self, other: list | any): list

Returns a new list containing the elements of the given list followed by the elements of the other list or the given value.

__set__(self, key: number, value: any): null

Sets the element at the given index to the given value.