Array/Vector Library
Stof's standard array/vector library ("Array").
In Stof, arrays and vectors are the same thing. We support both nomenclatures because it is common for "array" to be used in data formats, however, an array in most programming contexts is a fixed-capacity compound type that cannot grow in size. In Stof, arrays are implemented as vectors, and the "vec" type makes more sense. This separation also helps distinguish the arrays declared in a document from the "runtime" vectors used to manipulate data.
Anywhere a vec type is supported, a Box<vec> type is also supported. If a boxed value is passed in and manipulated, it will be manipulated anywhere else it is also referenced. See Primitive Types for more information on the Box type.
Common Value Functions
Array.toString(array: vec): str
Array.or(array: vec, ...): unknown
Returns the first non-empty (null or void) argument, just like the Standard Library "or" function.
Vector Functions
Array.append(array: vec, other: vec): void
Appends another vector into this array, leaving the other empty. If not boxed, "other" will be cloned when this function is called, and the original vector maintains its values.
Array.push(array: vec, ...): void
Pushes all arguments to the end of the array in order, as they are given.
Array.pop(array: vec, index?: int | unknown): unknown | null
Pop a value from this array. If an index is not provided, the last element of the array will be removed and returned (null if the array is empty).
If the index is a number, it will be treated as the position within the array to remove and return. An error will be thrown if this index is greater than or equal to the length of the array (indices start at zero).
If the index is a value other than a number, the first value in the array that equals the index value will be removed and returned. If no value in the array equals the index, null is returned.
It is preferred to use the "remove", "removeLast", or "removeAll" functions for removing values by equality rather than by index/position.
Array.reverse(array: vec): void
Reverses the array in place.
Array.reversed(array: vec): vec
Clones this array in the reverse order and returns it, leaving the original array unmodified.
Array.len(array: vec): int
The length of this array.
Array.empty(array: vec): bool
Is this array empty?
Array.any(array: vec): bool
Does this array have any values (not empty)?
Array.at(array: vec, index: int): unknown
Get a value in this array at a specific index. Will throw an error if the index is out of bounds.
Any value/object that implements an "at" function and a "len" function can be used with for-loops in Stof.
Boxing values inside an array will enable you to manipulate values without setting them.
Array.first(array: vec): unknown | null
Returns the first value in the array (cloned) or null if the array is empty.
Array.last(array: vec): unknown | null
Returns the last value in the array (cloned) or null if the array is empty.
Array.join(array: vec, separator: str): str
Joins the values inside this array into a singular string, separated by a "separator" value. This array is unmodified.
Array.contains(array: vec, value: unknown): bool
Does this array contain a value (equality comparison)?
Array.find(array: vec, other: unknown): int
Returns the index of the first value in this array to equal "other". If not found, returns -1.
Array.remove(array: vec, other: unknown): unknown | null
Removes the first value in the array to equal "other" and returns it. Returns null if not found.
Array.removeLast(array: vec, other: unknown): unknown | null
Removes the last value in the array to equal "other" and returns it. Returns null if not found.
Array.removeAll(array: vec, other: unknown): vec
Removes all values in the array that equal "other", returning a vector of all values removed.
Array.insert(array: vec, index: int, ...values: unknown): void
Inserts values into this array at a specific index, shifting existing elements to the right. Must provide at least one value to insert.
Will throw an error if the index is out of bounds or if you do not provide at least one value to insert.
Array.set(array: vec, index: int, ...values: unknown): void
Inserts values into this array at a specific index, replacing the existing element at that index. Must provide at least one value to insert.
Will throw an error if the index is out of bounds or if you do not provide at least one value to insert.
Array.iter(array: vec, func: fn): void
Iterates over this array, calling "func" for each value. If "func" returns a non-null value, that value is then set in place of the existing element. The function passed in must take a singular value as a parameter, in the type you know you are iterating over (or "unknown" to accept all).
Array.retain(array: vec, func: fn): void
For each value in this array, call "func" (passing the value in as the only argument) - if "func" returns true, keep the value, otherwise, remove the value from the array.
Array.sort(array: vec, func?: fn): void
Sort this array, optionally providing a function to sort with.
If a function is not provided, Stof will use the "less-than" and "greater-than" built-in functions to compare values.
If providing a function, the function should take two value arguments and return -1 for less than, 1 for greater than, or 0 for equal.
Last updated