On this page:
Array
Array.of_  length
Array.now_  of
Array.later_  of
Mutable  Array
Immutable  Array
Array
Array
Array
Array.of_  length
Array.make
Array.length
Array.get
Array.set
Array.append
Array.copy
Array.copy_  from
Array.take_  left
Array.take_  right
Array.drop_  left
Array.drop_  right
Array.set_  in_  copy
Array.to_  list
Array.to_  sequence
8.12

6.33 Arrays🔗ℹ

An array is indexable using [] to access an array element by position (in constant time) via #%index, and it also support element assignment via [] and :=. An array also works with the ++ operator to append arrays. An array can be used as sequence, in which case it supplies its elements in order.

An array is normally mutable, but immutable arrays can originate from Racket. The Array annotation is satisfied by both mutable and immutable arrays, while MutableArray and ImmutableArray require one or the other.

The . operator can be used on a array expression as equivalent to calling Array functions:

arr.length()

 is 

Array.length(arr)

arr.get(n)

 is 

Array.get(arr, n)

arr.set(n, v)

 is 

Array.set(arr, n, v)

arr.append(arr2, ...)

 is 

Array.append(arr, arr2, ...)

arr.copy(arg, ...)

 is 

Array.copy(arr, arg, ...)

arr.copy_from(arg, ...)

 is 

Array.copy_from(arr, arg, ...)

arr.take_left(n)

 is 

Array.take_left(arr, n)

arr.take_right(n)

 is 

Array.take_right(arr, n)

arr.drop_left(n)

 is 

Array.drop_left(arr, n)

arr.drop_right(n)

 is 

Array.drop_right(arr, n)

arr.set_in_copy(i, v)

 is 

Array.set_in_copy(arr, i, v)

arr.to_list()

 is 

Array.to_list(arr)

arr.to_sequence()

 is 

Array.to_sequence(arr)

annotation

Array

 

annotation

Array.of_length(expr)

 

annotation

Array.now_of(annot)

 

annotation

Array.later_of(annot)

 

annotation

MutableArray

 

annotation

ImmutableArray

The Array annotation (without of_length, now_of, or later_of) matches any array. The Array.of_length annotation matches arrays of a given length.

The Array.now_of form constructs a predicate annotation that matches an array whose elements all currently satisfy annot, but it does not ensure in any way that future values installed into the array will satisfy annot. The given annot must not be a converting annotation. Static information from annot is not propagated to accesses of the array, since there’s no guarantee that the value will still satisfy the annotation.

The Array.later_of form constructs a converter annotation that immediately matches an array without checking that its elements currently satisfy annot. The conversion result of the annotation is a view of the original array, but one where annot is checked against a value that would be returned by accessing an element of the array or a value to be installed into the array. (A different view of the array might change an element to one that does not astisfy annot.) Static information from annot is propagated to accesses of the array. Note that a converter annot is applied for each access or update.

MutableArray matches only mutable arrays, and ImmutableArray matches only immutable arrays (that may originate from Racket).

Static information associated by Array, etc., makes an expression acceptable as a sequence to for in static mode.

> Array(1, 2, 3) :: Array

Array(1, 2, 3)

> Array(1, 2, 3) :: Array.of_length(3)

Array(1, 2, 3)

> Array(1, 2, 3) :: Array.of_length(5)

::: value does not satisfy annotation

  value: Array(1, 2, 3)

  annotation: Array.of_length(5)

> Array(1, 2, 3) :: Array.now_of(Number)

Array(1, 2, 3)

> Array(1, "b", 3) :: Array.now_of(Number)

::: value does not satisfy annotation

  value: Array(1, "b", 3)

  annotation: Array.now_of(Number)

def a :: Array.later_of(Number) = Array(1, "b", 3)

> a[0]

1

> a[1]

Array: current element does not satisfy annotation

  current element: "b"

  position: 1

  annotation: Number

> a[2] := "c"

Array: new element does not satisfy annotation

  new element: "c"

  position: 2

  annotation: Number

function

fun Array(v :: Any, ...) :: MutableArray

Constructs a mutable array containing given arguments.

> def a = Array(1, 2, 3)

> a

Array(1, 2, 3)

> a[0]

1

> a[0] := 0

> a

Array(0, 2, 3)

binding operator

Array(bind, ...)

 

binding operator

Array(bind, ..., repet_bind , ellipsis)

 

ellipsis

 = 

...

Matches an array with as many elements as binds, where each element matches its corresponding bind, or at least as may elements as binds when a repet_bind is provided. When repet_bind is provided, each additional element must match repet_bind.

Elements are extracted from a matching array eagerly, so mutations of the array afterward do no change the matched values. When repet_bind is provided, the extracted matching elements are combined into an internal list to implement the repetition.

> def Array(1, x, y) = Array(1, 2, 3)

> y

3

> def Array(1, z, ...) = Array(1, 2, 3)

> [z, ...]

[2, 3]

reducer

Array

 

reducer

Array.of_length(len_expr, maybe_fill)

 

maybe_fill

 = 

~fill: fill_expr

 | 

ϵ

Reducers used with for, accumulates each result of a for body into a result array.

The Array.of_length reducer, like the corresponding annotation, produces an array of a given length. Specifically, an array of the specified length is created and mutated by iterations of the for body. Iterations more than the specified length will trigger an exception, while iterations fewer than the length will leave the value of fill_expr (or 0) in the array.

function

fun Array.make(length :: NonnegInt, val :: Any = 0)

  :: MutableArray

Creates a fresh array with length slots, where each slot is initialized to val.

> Array.make(3, "x")

Array("x", "x", "x")

function

fun Array.length(arr :: Array) :: Int

Returns the length of arr.

> Array.make(3, "x").length()

3

function

fun Array.get(arr :: Array, n :: NonnegInt) :: Any

Equivalent to arr[n] (with the default implicit #%index form). Returns the nth element of arr (starting from 0).

> Array("a", "b", "c")[1]

"b"

> Array("a", "b", "c").get(1)

"b"

function

fun Array.set(arr :: MutableArray,

              n :: NonnegInt, val :: Any)

  :: Void

Equivalent to arr[n] := val (with the default implicit #%index form). Updates the nth position of arr to val.

> def a = Array("a", "b", "c")

> a.set(1, "d")

> a

Array("a", "d", "c")

> a[1] := "e"

> a

Array("a", "e", "c")

function

fun Array.append(arr :: Array, ...) :: MutableArray

Appends arrs by creating a new mutable array with all elements.

> Array(1, 2, 3).append(Array(4, 5, 6), Array(7, 8, 9))

Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

function

fun Array.copy(arr :: Array,

               start :: NonnegInt = 0,

               end :: NonnegInt = Array.length(arr))

  :: MutableArray

Returns a fresh array string with the same initial content as in arr from position start (inclusive) through end (exclusive).

> Array("a", "b", "c").copy()

Array("a", "b", "c")

> Array("a", "b", "c").copy(1)

Array("b", "c")

> Array("a", "b", "c").copy(1, 2)

Array("b")

function

fun Array.copy_from(dest_arr :: MutableArray,

                    dest_start :: NonnegInt,

                    src_arr :: Array,

                    src_start :: NonnegInt = 0,

                    src_end :: NonnegInt = Array.length(src_arr))

  :: Void

Copies bytes from src_arr at src_start (inclusive) to src_end (exclusive) into dest_arr starting at dest_start. The length of dest_arr must be at least dest_start + (src_end - src_start).

function

fun Array.take_left(arr :: Array, n :: NonnegInt)

  :: MutableArray

 

function

fun Array.take_right(arr :: Array, n :: NonnegInt)

  :: MutableArray

 

function

fun Array.drop_left(arr :: Array, n :: NonnegInt)

  :: MutableArray

 

function

fun Array.drop_right(arr :: Array, n :: NonnegInt)

  :: MutableArray

Like Array.copy with a range that selects a prefix or suffix of arr.

> Array("a", "b", "c").take_left(2)

Array("a", "b")

> Array("a", "b", "c").take_right(2)

Array("b", "c")

> Array("a", "b", "c").drop_left(2)

Array("c")

> Array("a", "b", "c").drop_right(2)

Array("a")

function

fun Array.set_in_copy(arr :: Array,

                      n :: NonnegInt, val :: Any)

  :: MutableArray

Returns a new mutable array like arr, but with val as the nth element.

> Array("a", "b", "c").set_in_copy(1, "x")

Array("a", "x", "c")

function

fun Array.to_list(arr :: Array) :: List

Implements Listable by returning a list with the same elements as arr in the same order.

Implements Sequenceable by returning a sequence of arr’s elements in order.