Skip to content

@arrays

Import this module with import @arrays.

Function Signature Description
is_empty (arr [T]) -> bool Check if array is empty
contains (arr [T], value T) -> bool Check if value exists
index_of (arr [T], value T) -> i64 First index of value (-1 if not found)
count (arr [T], value T) -> i64 Count occurrences of value
is_equal (a [T], b [T]) -> bool Structural equality. Compares length first, then elements. T must be a primitive (an integer or float type, bool, char) or string; arrays of nested composites are rejected at compile time.
is_sorted (arr [T]) -> bool True if elements are in ascending order (each <= the next). Empty and single-element arrays are sorted. T must be comparable, as for sort_asc.
binary_search (arr [T], val T) -> i64 Search a sorted array for val (index, or -1 if absent). arr must already be sorted ascending, as by sort_asc; behavior on an unsorted array is undefined. T must be comparable, as for sort_asc.

The == and != operators on arrays are not allowed; use arrays.is_equal(a, b) for equality.

Function Signature Description
get_first (arr [T]) -> T Return first element (panic if empty)
get_last (arr [T]) -> T Return last element (panic if empty)
Function Signature Description
append (&arr [T], value T) Append element
prepend (&arr [T], value T) Insert value at front
insert_at (&arr [T], index i64, value T) Insert at index
remove (&arr [T], value T) Remove first occurrence of value
remove_at (&arr [T], index i64) Remove element at index
remove_first (&arr [T]) -> T Remove and return first element (panic if empty)
remove_last (&arr [T]) -> T Remove and return last element (panic if empty)
clear (&arr [T]) Remove all elements
fill (&arr [T], value T, count i64) Fill array with N copies of value
sort_asc (&arr [T]) Sort ascending in-place
sort_desc (&arr [T]) Sort descending in-place
swap (&arr [T], i i64, j i64) Swap the elements at i and j in place; panics if either index is out of bounds
Function Signature Description
reverse (arr [T]) -> [T] Return reversed copy
rotate (arr [T], n i64) -> [T] Return a copy rotated left by n (negative n rotates right)
slice (arr [T], start i64, end i64) -> [T] Return slice
concat (a [T], b [T]) -> [T] Concatenate two arrays
deduplicate (arr [T]) -> [T] Remove duplicate values
flatten (arr [[T]]) -> [T] Flatten one level of nesting
split_every (arr [T], size i64) -> [[T]] Split into sub-arrays of given size
pair (a [T], b [T]) -> [[T]] Pair elements from two arrays
Function Signature Description
get_sum (arr [T]) -> T Sum all elements. Accepts any integer or float type.
get_min (arr [T]) -> T Minimum element
get_max (arr [T]) -> T Maximum element
min_index (arr [T]) -> i64 Index of the minimum element, or -1 if arr is empty
max_index (arr [T]) -> i64 Index of the maximum element, or -1 if arr is empty
average (arr [T]) -> f64 Arithmetic mean as an f64. T must be numeric; panics on an empty array.
Function Signature Description
map (arr [T], transform func(T) -> T) -> [T] Returns a new array with transform applied to each element.
filter (arr [T], predicate func(T) -> bool) -> [T] Returns a new array containing only elements for which predicate returns true.
reduce (arr [T], initial T, accumulator func(T, T) -> T) -> T Reduces the array to a single value by applying accumulator(acc, element) for each element, starting with initial.
any (arr [T], predicate func(T) -> bool) -> bool Returns true if at least one element satisfies predicate. Returns false on an empty array.
all (arr [T], predicate func(T) -> bool) -> bool Returns true if every element satisfies predicate. Returns true on an empty array.
find (arr [T], predicate func(T) -> bool) -> (T, bool) Returns the first matching element and true, or the zero value and false. Must be destructured.
find_index (arr [T], predicate func(T) -> bool) -> i64 Returns the index of the first matching element, or -1.