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.contains
Array.append
Array.copy
Array.copy_  from
Array.snapshot
Array.take
Array.take_  last
Array.drop
Array.drop_  last
Array.set_  in_  copy
Array.to_  list
Array.to_  sequence
8.16.0.2

8.4 Arrays🔗ℹ

An array is indexable using [] to access an array element by position (in constant time) via #%index, and it also supports element assignment via [] and :=. An array also works with the ++ operator to append arrays. An array supports membership tests using the in operator. 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 or Array.snapshot. The Array annotation is satisfied by both mutable and immutable arrays, while MutableArray and ImmutableArray require one or the other.

Two arrays are equal by is_now as long as they have equal contents, even if one is mutable and the other is immutable.

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 satisfy 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

 = 

...

 | 

... ~nonempty

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, and at least one match must be present if ellipsis includes ~nonempty.

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]

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

> [z, ...]

[2, 3]

> def Array(1, z, ... ~nonempty) = Array(1)

def: value does not satisfy annotation

  value: Array(1)

  annotation: matching(Array(1, _, ...))

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")

method

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

Returns the length of arr.

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

3

method

method (arr :: Array).get(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"

method

method 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")

method

method (arr :: Array).contains(val :: Any,

                               eqls :: Function.of_arity(2) = (_ == _))

  :: Boolean

List List.contains, but for arrays. See also in.

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

#true

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

#true

method

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

 

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)

> Array.append()

Array()

method

method (arr :: Array).copy(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).

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

> a.copy()

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

> a.copy(1)

Array("b", "c")

> a.copy(1, 2)

Array("b")

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

> a.copy()

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

> a.copy() is_now a

#true

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).

Returns an immutable array as-is or copies a mutable array’s content to an immutable array.

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

> a.snapshot()

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

> a.snapshot() is_now a

#true

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

> a.snapshot()

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

> a.snapshot() === a

#true

method

method (arr :: Array).take(n :: NonnegInt)

  :: MutableArray

 

method

method (arr :: Array).take_last(n :: NonnegInt)

  :: MutableArray

 

method

method (arr :: Array).drop(n :: NonnegInt)

  :: MutableArray

 

method

method (arr :: Array).drop_last(n :: NonnegInt)

  :: MutableArray

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

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

Array("a", "b")

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

Array("b", "c")

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

Array("c")

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

Array("a")

method

method (arr :: Array).set_in_copy(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")

method

method (arr :: Array).to_list() :: 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.