Class List

(builtin) class List[A] {  }

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.

Methods

define clear

Removes all elements present within self. No error is raised if self is being iterated over.

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.

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 if index is out of range.

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.

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.

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.

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.

define insert(index: Integer, value: 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 if index is not within self.

define join(separator: *String = ""): String

Create a String consisting of the elements of self interleaved with separator. The elements of self are converted to a String as if they were interpolated. If self is empty, then the result is an empty String.

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.

define pop: A

Attempt to remove and return the last element within self.

  • IndexError if self is empty.

define push(value: A)

Add value to the end of self.

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.

define repeat(count: Integer, value: A): List[A] static

This creates a new List that contains value repeated count times.

  • ValueError if count is less than 1.

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.

define size: Integer

Returns the number of elements that are within self.

define shift: A

This attempts to remove the last element from self and return it.

  • ValueError if self is empty.

define slice(start: *Integer = 0, stop: *Integer = -1): 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 or stop is out of range.
  • The start is larger than the stop (reversed).

define unshift(value: A)

Inserts value at the front of self, moving all other elements to the right.