# Arrays Arrays are Deft's mutable collection: a flat block of values with O(1) indexed reads and writes through `aget` and `aset`. They are the deliberate exception to the persistent collections covered in Collections — every other collection returns a new value on update, while arrays mutate in place. They are stdlib natives, available in every host with no import, and display as `@{...}`. Convert one back to a persistent list with `collect` when the hot loop is over. ## Functions | Function | Purpose | |---|---| | `array values...` | Create a mutable array; with a sole numeric argument, a nil-filled array of that length | | `array-of n v` | Pre-filled array: `n` copies of `v` in one allocation | | `aget arr i` | Read the element at zero-based index `i` (O(1)) | | `aset arr i v` | Write `v` at index `i`, in place; returns the array | Notes: - `[array 5]` is a nil-filled array of length 5, not a one-element array. The numeric argument is treated as a size only when it is the sole argument; `[array 5 6]` holds the two elements 5 and 6. - `aget` also accepts vectors (`@{...}`). Out-of-range indices, negative indices, and non-indexable targets return `nil` rather than raising; strings and maps are not indexable with `aget` (use `nth` / `get`). - `aset` mutates the same object in place and returns it for arrays and vectors; out-of-range writes are silent no-ops. A non-numeric index raises `%Err{:message expected number argument}`. `set-nth` is the persistent alternative that returns a new list. - Arrays do not implement the full collection protocol; `collect` converts one to a persistent list. ## Examples
# Create
def empty [array]                 # @{}
def nils  [array 5]               # @{nil nil nil nil nil}
def nums  [array 1 2 3]           # @{1 2 3}
def zeros [array-of 4 0]          # @{0 0 0 0}

# Read
[aget $nums 0]                    # => 1
$nums(aget 1)                     # => 2 (postfix form)
[aget $nums 99]                   # => nil (out of range)

# Mutate in place
[aset $nums 1 99]                 # => @{1 99 3}
[aget $nums 1]                    # => 99
[aset $nums 99 0]                 # out of range — silently ignored

# Build a buffer in a loop
def squares [array-of 4 0]
for i in [range 0 4] { [aset $squares $i ($i * $i)] }
$squares                          # => @{0 1 4 9}
[collect $squares]                # => @{0 1 4 9} (persistent list copy)
## See Also - Collections — lists, maps, and sets, the persistent default - Lists — persistent list operations - Maps — persistent map operations