list¶
Reductions, slicing, and higher-order methods on arrays.
The module is part of the prelude, so every method
here works on any array with no import line: [1, 2, 3].sum(), xs.map(f). The methods are defined in an
impl list block; see methods for how impl blocks
on the built-in types resolve.
The helpers are polymorphic through compile-time monomorphisation: one definition
specialises to whatever element type the call site uses. A lookup that may find
nothing (first, last, min, max, find) answers an Option:
Some of the element, or None for an empty list or no match. The reductions sum and product
read their seed from the first element, so they need a non-empty list.
The module is pure candela, so it compiles into a .cdlb artifact and runs under
candela-vm with no dynamic library. It builds on the built-in array methods
(len, push, contains, index_of, sort, and the rest), which are listed in
built-in functions.
Element access¶
first¶
- Returns:
Someof the element at index 0, orNonefor an empty list.
last¶
- Returns:
Someof the element at indexarr.len() - 1, orNonefor an empty list.
is_empty¶
- Returns: a bool, true when
arrhas no elements.
count¶
value: an element to look for.- Returns: the number of elements equal to
value, as an int.
Reductions¶
sum¶
- Returns: the elements combined left to right with
+. - Raises:
index_out_of_boundson an empty list.
The element type only has to support +, so a list of strings sums to their
concatenation.
product¶
- Returns: the elements combined left to right with
*. - Raises:
index_out_of_boundson an empty list.
min¶
- Returns:
Someof the smallest element, compared with<, orNonefor an empty list.
The element type has to be one < orders: int, float, or string. A list
of strings answers with the first one in byte order, the same order sort
uses; see operators.
max¶
- Returns:
Someof the largest element, compared with>, orNonefor an empty list.
The element type has to be one > orders, as for min.
Slicing and reshaping¶
take¶
n: an int.- Returns: a new list of the first
nelements, or a copy of the whole list whennexceeds the length. An empty list gives an empty list.
drop¶
n: an int.- Returns: a new list of every element after the first
n, and an empty list whennreaches or exceeds the length.
chunk¶
size: an int.- Returns: a new list of consecutive sub-lists of at most
sizeelements. The final chunk is shorter when the length is not a multiple ofsize. An empty list gives an empty result.
unique¶
- Returns: a new list with duplicate elements removed, keeping first-seen order. An empty list returns unchanged.
Higher-order helpers¶
Each of these takes a function value. Pass a named function or an anonymous one; see functions.
map¶
f: takes one element, returns the mapped value.- Returns: a new list of the results, in order.
filter¶
f: takes one element, returns a bool.- Returns: a new list of the elements for which
fis true, in order.
reduce¶
init: the starting accumulator.f: takes the accumulator and an element, returns the new accumulator.- Returns: the accumulator after folding left to right.
initon an empty list.
each¶
f: takes one element; its result is discarded.- Returns: nothing. Call it for the side effect.
find¶
f: takes one element, returns a bool.- Returns:
Someof the first element for whichfis true, orNonewhen none match.
The position of a value is the built-in arr.index_of(x), listed in
built-in functions. find with an argument that is not a
function is a compile error naming it.
any¶
f: takes one element, returns a bool.- Returns: true when
fholds for at least one element. False on an empty list.
all¶
f: takes one element, returns a bool.- Returns: true when
fholds for every element. True on an empty list.
sort_by¶
less: takes two elements, returns true when the first comes before the second.- Returns: a new sorted list;
arris left unchanged.
A stable insertion sort. Use it when you need a custom order or an untouched
input; the built-in arr.sort() sorts ints, floats, and strings ascending in
place.