Lists

This page documents the List type.

General concepts

A list is an ordered collection of values, which may be of different types. Elements in a list can be added and removed, and the list grows dynamically to accomodate new incoming elements. Indices in a list start at 1 and can be negative: -1 represents the last element, -2 the second-to-last element, and so on.

Methods

class List
append(elem[, ...])

Appends one or more elements at the end of the list.

See also: prepend(), insert()


clear()

Empty the content of the list. After this method is called, a call to is_empty() will return true.


clone()

Returns a shallow copy of the list.


concat(arg[, ...])

Returns a new list, which is the concatenation of this list with one or more lists.


contains(elem)

Returns true if elem is in the list and false otherwise.


every(callback)

Returns true if all the elements in the list satisfy the condition callback, which must be a function that takes a value as input and returns a Boolean.

function greater_than_10(x)
    return x > 10
end
var lst = [11, 12, 13]

# Prints "true"
print(lst.every(greater_than_10))

See also: some()


filter(callback)

Returns a new list containing the elements that satisfy the condition callback, which must be a function that takes a value as input and returns a Boolean.

function is_odd(x)
    return x % 2 == 1
end

var lst = [1, 2, 3, 4, 5, 6]
print(lst.filter(is_odd)) # prints [1, 3, 5]

See also: map()


find(elem[, i])

Returns the index of elem in the list, starting the search at index i (1 by default). If the element is not found, 0 is returned.

Note: If the list is sorted, you can use sorted_find() instead, which is a little faster since it can take advantage of the fact that the order of the elements is known.

See also: find_back(), sorted_find()


find_back(elem[, i])

Returns the index of elem in the list, starting the search from the end at index i (-1 by default). If the element is not found, 0 is returned.

See also: find()


includes(other)

Returns true if the elements in other are a subsequence this list. Both lists must be sorted, but the elements in other don’t need to be a contiguous sequence.


insert(i, elem)

Inserts the element elem at index i.

See also: sorted_insert()


intersect(other)

Returns a new list which contains all the elements that are in this list and in other.

See also:: unite(), subtract()


is_empty()

Returns true if the list doesn’t contain any element, and false if it does.


is_sorted()

See also: sort(), reverse()


join(sep)

Returns a string in which all elements have been joined with the sep separator.


map(callback)

Returns a new list in which callback has been applied to each element in the original list. callback must be a function that takes a value and returns a value.

function upper(s)
    return s.to_upper()
end
var lst = ["a", "b", "c"]
print(lst.map(upper))

See also: filter()


pop()

Removes the last element from the list and returns it.

See also: shift()


prepend(elem[, ...])

Inserts one or more elements at the beginning of the list.

See also: append(), insert()


reduce(callback)

Reduces the list to a single value, by applying callback to each element in the list from beginning to end. callback must be a function which takes two arguments (an accumulator and a value) and returns a single value.

function callback(accumulator, value)
    return accumulator + value
end

var lst = [1, 2, 3, 4]
print(lst.reduce(callback)) # prints 10

See also: reduce_back()


reduce_back(callback)

Reduces the list to a single value, by applying callback to each element in the list from end to beginning. callback must be a function which takes two arguments (an accumulator and a value) and returns a single value.

See also: reduce()


remove(value)

Removes all the elements in the list that are equal to value.

See also: remove_at()


remove(i)

Remove the element at index i.

See also: remove()


reverse()

Reverses the order of the elements in the list. If the elements are not sorted, the result is undefined. (Use sort() to sort the elements.)

See also: is_sorted(), sort()


sample(n)

Returns a list containing n elements from the list drawn at random.


shift()

Removes the first element from the list and returns it.

See also: pop()


..method:: shuffle()

Randomizes the order of elements in the list.


slice(from, to)

Returns a new list which contains the elements of the original list starting at index from and ending at index to (inclusive).


some(callback)

Returns true if at least one element in the list satisfies the condition callback, which must be a function that takes a value as input and returns a Boolean.

function less_than_10(x)
    return x < 10
end
var lst = [9, 11, 12, 13]

# Prints "true"
print(lst.every(greater_than_10))

See also: every()


sort()

Sorts the elements in the list in increasing order. The elements should be of the same type.

See also: is_sorted(), reverse()


sorted_find(value)

Finds the index of value in a sorted list. If value is not in the list, 0 is returned. Note that if the list is not sorted, the result of this operation is undefined.

This method is faster than find() on average for sorted lists.

See also: find()


sorted_insert(value)

Inserts value after the first element that is not less than elem. If the list is not sorted, the result of this operation is undefined.

See also: insert()


subtract(other)

Returns a new list which contains all the elements that are in this list but not in other.

See also:: intersect(), unite()


to_string()

Returns a string representation of the list.


unite(other)

Returns a new list which contains all the elements that are either in this list or in other (or in both).

See also:: intersect(), subtract()

Fields

length

Returns the number of elements in the list.