x-lang

← Index

x/type/vector

Fixed-size indexed vectors; operations homed on the Vector class.

Literal syntax: #(1 2 3), with negative indexing via the vector’s call slot.

Class Vector

(Vector of . args)

Create a vector from the given arguments.

Returns: VECTOR — New vector containing the arguments

Examples:

(Vector of 1 2 3) => #(1 2 3)

(Vector make n fill)

Create a vector of length n, with every element set to fill.

Parameters:

Returns: VECTOR — New vector of length n filled with fill

(Vector build n f)

Create a vector of length n where element i is (f i). Built in place – no intermediate list.

Block form: (Vector build n (x) body …) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: VECTOR — New vector of length n, element i = (f i)

Examples:

(Vector build 3 (fn (_ i) (* i i))) => #(0 1 4)

(Vector vector? x)

Test whether a value is a vector.

Parameters:

Returns: BOOL — True if x is a vector

(Vector ref i v)

Return the element at index i of a vector; negative i counts from the end. Errors when i is out of range.

Parameters:

Returns: ANY — Element at index i

(Vector set! i x v)

Store x at index i of a vector (in place); negative i counts from the end. Errors when i is out of range; returns the vector for chaining.

Parameters:

Returns: VECTOR — v

Examples:

(Vector ref 0 (Vector set! 0 99 (Vector of 1 2))) => 99

(Vector length v)

Return the number of elements in a vector.

Parameters:

Returns: INT — Number of elements

(Vector ->list v)

Convert a vector to a list.

Parameters:

Returns: LIST — List of the vector’s elements

(Vector from-list lst)

Convert a list to a vector.

Parameters:

Returns: VECTOR — New vector containing the list’s elements

(Vector iter v)

An iterator over the vector’s elements.

Parameters:

Returns: ITER — Iterator

(Vector for-each f v)

Apply f to each element in order, for its side effects; returns nil. Named for-each, matching List/Iter/Seq.

Block form: (Vector for-each (x) body … v) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: ANY — nil

(Vector map f v)

A new vector of (f element), in order. Built in place – no intermediate list.

Block form: (Vector map (x) body … v) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: VECTOR — New vector of the same length

Examples:

(Vector map (fn (_ x) (* x 2)) (Vector of 1 2 3)) => #(2 4 6)

(Vector filter pred v)

A new vector of the elements satisfying pred, in order.

Block form: (Vector filter (x) body … v) – or (i x) for the 0-based index, then the element.

Parameters:

Returns: VECTOR — New vector, possibly shorter

Examples:

(Vector filter (fn (_ x) (> x 1)) (Vector of 1 2 3)) => #(2 3)

(Vector fold f acc v)

Left-fold: thread acc through the elements, left to right.

Block form: (Vector fold (acc x) body … acc v) – or (acc i x) with the 0-based index ahead of the element.

Parameters:

Returns: ANY — The final accumulator

Examples:

(Vector fold + 0 (Vector of 1 2 3)) => 6