x-lang

← Index

x/type/array

Array: the growable container – amortized O(1) push!, O(1) indexed access, over a vector backing store.

Backing VECTOR doubles on overflow; slot 0 of the backing store is its capacity.

Class Array

A growable container: amortized O(1) push!, O(1) ref/set!, backed by a doubling VECTOR.

Mutators (push!/set!) return the array for chaining; pop! returns the removed element.

store

Field: data carried by a Array instance.

len

Field: data carried by a Array instance.

(Array make . opt)

An empty array. Pass a capacity to pre-size the backing store.

Parameters:

Returns: Array — A new empty array

Examples:

((Array make) length) => 0
((Array make 64) empty?) => #t

(Array from-list lst)

Build an array from a list’s elements.

Parameters:

Returns: Array — An array holding the list’s elements

Examples:

((Array from-list (list 1 2 3)) length) => 3

(Array of . args)

Variadic literal: an array of the arguments.

Parameters:

Returns: Array — An array holding the arguments

Examples:

((Array of 1 2 3) ->list) => (1 2 3)

(%live)

Instance method: called on a Array instance.

(%index i what)

Instance method: called on a Array instance.

(push! x)

Append an element (doubling the backing store when full); returns the array for chaining.

Instance method: called on a Array instance.

Parameters:

Returns: Array — self

Examples:

(((Array of 1 2) push! 3) ->list) => (1 2 3)

(pop!)

Remove and return the last element; errors when empty.

Instance method: called on a Array instance.

Returns: ANY — The removed element

Examples:

(let ((a (Array of 1 2 3))) (list (a pop!) (a length))) => (3 2)

(ref i)

The element at index i (negative counts from the end); errors out of range.

Instance method: called on a Array instance.

Parameters:

Returns: ANY — Element at i

Examples:

((Array of 'a 'b 'c) ref 1) => 'b
((Array of 'a 'b 'c) ref -1) => 'c

(set! i x)

Store x at index i (in place; negative counts from the end); errors out of range; returns the array for chaining.

Instance method: called on a Array instance.

Parameters:

Returns: Array — self

Examples:

(((Array of 1 2 3) set! 0 9) ->list) => (9 2 3)

(length)

The live element count.

Instance method: called on a Array instance.

Returns: INTEGER — Element count

Examples:

((Array of 1 2 3) length) => 3

(empty?)

Test whether the array holds no elements.

Instance method: called on a Array instance.

Returns: BOOL — #t when empty

Examples:

((Array make) empty?) => #t
((Array of 1) empty?) => #f

(->list)

The elements as a list, in order.

Instance method: called on a Array instance.

Returns: LIST — List of elements

Examples:

((Array of 'x 'y) ->list) => ('x 'y)