Skip to content

Array[T]

Arrays are ordered, mutable sequences of values.

The element type is invariant. Use BaseArray for a covariant read view, or Seq to accept other sequences too.

Inherits from: BaseArray[T], Sinkable[T]

Implements: MutSeq[T]

Constructor

Array iterable

Builds an array from one iterable. The lowercase array factory instead collects its positional arguments verbatim.

Parameters

NameTypeDescription
iterable Iterable[T]

Methods

(assign)[K] key value overload 1 of 2

Replaces an element, or the elements of a slice.

let arr = [0, 1, 2, 3]
arr[1..3] = [9, 9]
assert_eq $arr [0, 9, 9, 3]

arr[1..1] = (tuple 4 5)
assert_eq $arr [0, 4, 5, 9, 9, 3]

A slice's replacement must be an Array or a Tuple, and its length may differ from the slice's. Any other value, including a Range or an iterator, raises TypeError. A slice with a step other than 1 can't be assigned and raises IndexError.

Parameters

NameTypeDescription
key K
value AssignItem[{*T}, K]

(assign) range value overload 2 of 2

Replaces an element, or the elements of a slice.

let arr = [0, 1, 2, 3]
arr[1..3] = [9, 9]
assert_eq $arr [0, 9, 9, 3]

arr[1..1] = (tuple 4 5)
assert_eq $arr [0, 4, 5, 9, 9, 3]

A slice's replacement must be an Array or a Tuple, and its length may differ from the slice's. Any other value, including a Range or an iterator, raises TypeError. A slice with a step other than 1 can't be assigned and raises IndexError.

Parameters

NameTypeDescription
range Range[Int]
value (BaseArray[T] | Tuple[...T])

(index)[K] key -> IndexItem[{*T}, K] overload 1 of 2

Returns an element, or a new array for a slice.

Parameters

NameTypeDescription
key K

(index) range -> Array[T] overload 2 of 2

Returns an element, or a new array for a slice.

Parameters

NameTypeDescription
range Range[Int]

clear()

Removes all elements from the array.

let arr = [1, 2, 3]
arr.clear()
assert_eq $arr.len 0

copy() -> Array[T]

Returns a shallow copy, as BaseArray.copy does.

delete index -> Bool

Indicates whether an element was removed. Deletes the element at index if it exists. Negative indexes count from the end.

Out-of-bounds indexes are ignored.

Parameters

NameTypeDescription
index Int

Example

let arr = [10, 20, 30]
assert (arr.delete 1)
assert (arr.delete -1)
assert (!(arr.delete 99))
assert_eq $arr [10]

insert index ...values

Inserts one or more values at the specified index, shifting existing elements. Negative indexes count from the end; -1 inserts before the last element.

Parameters

NameTypeDescription
index Int the position to insert at
...values T values to insert

Example

let arr = [1, 2, 3]
arr.insert 1 42
assert_eq $arr [1, 42, 2, 3]
arr.insert -1 99
assert_eq $arr [1, 42, 2, 99, 3]

pop[D = Never] … -> (T | D)

Removes and returns the last element, or the element at index if provided. Negative indexes count from the end.

Parameters

NameTypeDescription
index? Int optional index to remove; defaults to the last element
:default? D
:else? (() -> D)

Errors

Raises IndexError if the element does not exist and there is no default or else.

Example

let arr = [1, 2, 3]
assert_eq $arr.pop() 3
assert_eq $arr [1, 2]
assert_eq $arr.pop(0) 1

let empty = []
assert_eq (empty.pop default: "none") "none"

push ...values

Appends one or more values to the end of the array.

Parameters

NameTypeDescription
...values T values to append

Example

let arr = [1, 2]
arr.push 3
assert_eq $arr [1, 2, 3]

sort …

Sorts the array in place.

Parameters

NameTypeDescription
:key? ((T) -> Value)
:reverse? Bool

Example

let arr = ["bbb", "a", "cc"]
arr.sort key: (do |x| x.len)
assert_eq $arr ["a", "cc", "bbb"]

arr.sort reverse: true
assert_eq $arr ["cc", "bbb", "a"]
Open in playground