Skip to content

BaseArray[T]

The read-only half of an Array, whose elements it holds covariantly: an Array[Str] is a BaseArray[Str | Sym].

Use BaseArray for inputs that only read an array. Unlike Seq, it requires an actual array. It is Array at runtime; the view neither prevents mutation through another reference nor takes a snapshot.

Inherits from: Iterable[T]

Implements: Seq[T]

Fields

len @ Int [get]

Returns the number of elements.

Example
assert_eq $[1, 2, 3].len 3

Methods

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

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

let arr = [10, 20, 30]
assert_eq $arr[0] 10
assert_eq $arr[-1] 30

An index out of bounds raises IndexError; get returns a default instead.

Arrays also accept Range values for slicing:

let arr = [0, 1, 2, 3]
assert_eq $arr[1..3] [1, 2]
assert_eq $arr[..2] [0, 1]
assert_eq $arr[2..] [2, 3]
assert_eq $arr[..] [0, 1, 2, 3]
assert_eq $arr[Range 0 4 2] [0, 2]
assert_eq $arr[Range nil nil -1] [3, 2, 1, 0]
Open in playground

Omitted start means 0, omitted end means the array length, and negative start and end values count from the end. Negative steps reverse the slice.

Parameters

NameTypeDescription
key K

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

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

let arr = [10, 20, 30]
assert_eq $arr[0] 10
assert_eq $arr[-1] 30

An index out of bounds raises IndexError; get returns a default instead.

Arrays also accept Range values for slicing:

let arr = [0, 1, 2, 3]
assert_eq $arr[1..3] [1, 2]
assert_eq $arr[..2] [0, 1]
assert_eq $arr[2..] [2, 3]
assert_eq $arr[..] [0, 1, 2, 3]
assert_eq $arr[Range 0 4 2] [0, 2]
assert_eq $arr[Range nil nil -1] [3, 2, 1, 0]
Open in playground

Omitted start means 0, omitted end means the array length, and negative start and end values count from the end. Negative steps reverse the slice.

Parameters

NameTypeDescription
range Range[Int]

(iter)() -> UnpackIter[T]

Returns an iterator over the elements.

for value = [1, 2, 3]
  echo $value

(spread)() -> Spread[T]

Spreads the items positionally.

(unpack)() -> Unpack[T, Rest: BaseSeq[T]]

Destructures the array positionally.

let a b ...rest = [1, 2, 3, 4]
assert_eq $a 1
assert_eq $b 2

contains element -> Bool

Tests whether the array contains the given element (by equality).

Parameters

NameTypeDescription
element Value the value to check

Example

let arr = [1, 2, 3, "hello"]
assert (arr.contains 2)
assert (arr.contains "hello")
assert (!arr.contains 4)
assert (![].contains 1)

copy() -> BaseArray[T]

Returns a shallow copy of the array. Contents are not copied recursively.

When inherited by a Do subclass, copy() calls the subclass constructor with the source array as a single positional argument.

get[D = nil] index … -> (T | D)

Retrieves the value at the given index. Returns nil if out of bounds and no alternative is provided. Negative indexes count from the end.

Parameters

NameTypeDescription
index Int the index to access
:default? D
:else? (() -> D)

Example

let arr = [10, 20, 30]
assert_eq (arr.get 0) 10
assert_eq (arr.get -1) 30
assert_eq (arr.get 5 default: "missing") "missing"
assert_eq (arr.get 5 else: do "computed") "computed"

iter() -> UnpackIter[T]

Returns an iterator with read-only unpacking.

pairs() -> UnpackIter[Tuple[Int, T]]

Returns an iterator yielding (index, value) pairs.

Example

for i v = [10, 20, 30].pairs()
  echo "$i: $v"
# 0: 10
# 1: 20
# 2: 30