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 | |
| |
annotation | |
| |
annotation | |
| |
annotation | |
| |
annotation | |
| |
annotation | |
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(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)
> 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
> a
Array(1, 2, 3)
> a[0]
1
> a[0] := 0
> a
Array(0, 2, 3)
binding operator | ||||||||
| ||||||||
binding operator | ||||||||
| ||||||||
| ||||||||
|
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.
> y
3
> [z, ...]
[2, 3]
> [z, ...]
[2, 3]
def: value does not satisfy annotation
value: Array(1)
annotation: matching(Array(1, _, ...))
reducer | ||||||||
| ||||||||
reducer | ||||||||
| ||||||||
|
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 | ||
> Array.make(3, "x")
Array("x", "x", "x")
> Array.make(3, "x").length()
3
> Array("a", "b", "c")[1]
"b"
"b"
> a.set(1, "d")
> a
Array("a", "d", "c")
> a[1] := "e"
> a
Array("a", "e", "c")
method | |||
#true
#true
method | |
| |
| |
function | |
|
Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
> Array.append()
Array()
method | |||
|
function | ||||||
|
> a.snapshot()
Array.snapshot(Array("a", "b", "c"))
#true
> a.snapshot()
Array.snapshot(Array("a", "b", "c"))
#true
method | ||
| ||
method | ||
| ||
method | ||
| ||
method | ||
Array("a", "b")
Array("b", "c")
Array("c")
Array("a")
Array("a", "x", "c")