Class List
The List
class represents a container of a given type, written as
List[<inner type>]
. A List
value can be accessed through a positive
index or a negative one (with negative indexes being an offset from the
end). Attempting to access an invalid index will produce IndexError
.
public define accumulate(output: List[B], fn: Function(List[B], A)): List[B]
Build a transformed List
with a predicate.
This calls fn
for each element in self
, with output
as the first
argument each time. Unlike map, fn
does not have to return a value for
every element in self
.
This method is for situations where elements in a source dataset do not map 1 to 1 with the result dataset.
public define all(fn: Function(A => Boolean)): Boolean
This calls fn
for each element within self
.
Returns true
if fn
returns true
for all elements given, or if
self
is empty.
Otherwise, this returns false
.
public define any(fn: Function(A => Boolean)): Boolean
This calls fn
for each element within self
.
Returns true
if fn
returns true
for any element in self
, or if
self
is empty.
Otherwise, this returns false
.
public define clear
Removes all elements present within self
. No error is raised if self
is being iterated over.
public define count(fn: Function(A => Boolean)): Integer
This calls fn
for each element within self
. The result of this
function is the number of times that fn
returns true
.
public define delete_at(index: Integer)
Attempts to remove index from the List. If index is negative, then it is considered an offset from the end of the List.
IndexError
ifindex
is out of range.
public define each(fn: Function(A)): List[A]
Calls fn
for each element within self
. The result of this function
is self
, so that this method can be chained with others.
public define each_index(fn: Function(Integer)): List[A]
Calls fn
for each element within self
. Rather than receive the
elements of self
, fn
instead receives the index of each element.
public define each_with_index(fn: Function(A, Integer)): List[A]
Iterate over elements with indexes.
Calls fn
for each element within self
. fn
is given the elements
as well as their indexes.
public static define fill(count: Integer, fn: Function(Integer => A)): List[A]
Generate a List
of 'count' items using 'fn'.
This calls 'fn' with an index that starts at 0
and proceeds until
'count', and does not include 'count'.
If 'count' is 0
or negative, then the resulting List
will be empty.
public define fold(start: A, fn: Function(A, A => A)): A
This calls fn
for each element present within self
. The first value
sent to fn
is initially start
, but will later be the result of fn
.
Therefore, the value as it accumulates can be found in the first value
sent to fn
.
The result of this function is the result of doing an accumulation on
each element within self
.
public define get(index: Integer): Option[A]
Attempt to find index
within self
.
If the index is within self
, then the value is returned within a
Some
.
Otherwise, this returns None.
public define insert(index: Integer, value: A): List[A]
Attempt to insert value
at index
within self
. If index is
negative, then it is treated as an offset from the end of self
.
IndexError
ifindex
is not withinself
.
public define join(separator: *String): String
Create a String
consisting of the elements of self
interleaved with
separator
.
The elements of self
are converted into String
values in the same
way that ++
stringifies values.
If self
is empty, the result is an empty String
.
public define map(fn: Function(A => B)): List[B]
This calls fn
on each element within self
. The result of this
function is a newly-made List
containing the results of fn
.
public define map_with_index(fn: Function(A, Integer => B)): List[B]
Map elements with indexes.
This is equivalent to map, except that fn
receives indexes as well.
public define merge(others: List[List[A]]): List[A]
Create a new List
containing every element in self
and others
.
public define pop: A
public define push(value: A): List[A]
Add value
to the end of self
.
public define reject(fn: Function(A => Boolean)): List[A]
This calls fn
for each element within self
. The result is a newly-
made List
holding each element where fn
returns false
.
public static define repeat(count: Integer, value: A): List[A]
This creates a new List
that contains value
repeated count
times.
ValueError
ifcount
is less than 1.
public define reverse: List[A]
Return a new List
that contains all elements within self
ordered
from last to first.
public define select(fn: Function(A => Boolean)): List[A]
This calls fn
for each element within self
. The result is a newly-
made List
holding each element where fn
returns true
.
public define shift: A
This attempts to remove the last element from self
and return it.
ValueError
ifself
is empty.
public define size: Integer
Returns the number of elements that are within self
.
public define slice(start: *Integer, stop: *Integer): List[A]
Create a new List
copying a section of self
from start
to stop
.
If a negative index is given, it is treated as an offset from the end of
self
, with -1
being considered the last element.
On error, this generates an empty List
. Error conditions are:
- Either
start
orstop
is out of range.
- The
start
is larger than thestop
(reversed).
public define unshift(value: A): List[A]
Inserts value at the front of self, moving all other elements to the right.
public define zip(others: List[List[$1]]): List[Tuple[A, $1]]
This creates a List
that contains a merger of the values within each
of the elements in 'others' and 'self'.
The $1
type is a special type that allows this method to work with any
number of List
values.
If 'self' is List[Integer]
and 'others' is List[String]
and
List[Double]
, then the resulting type is
List[Tuple[Integer, String, Double]]
.
The size of the result List
is the same as the smallest List
provided.