arrays Module
The arrays module provides a comprehensive set of functions for array manipulation.
Functions
length(array)
Returns the number of elements in an array.
Parameters: - array: An array.
Returns: - (integer, error): The length of the array.
Example:
| Array Length | |
|---|---|
contains(array, value)
Checks if a value exists in an array.
Parameters: - array: An array. - value: The value to search for.
Returns: - (boolean, error): true if the value is found, false otherwise.
Example:
| Check Contains | |
|---|---|
index(array, value)
Returns the index of the first occurrence of a value in an array.
Parameters: - array: An array. - value: The value to search for.
Returns: - (integer, error): The index of the value, or -1 if not found.
Example:
| Find Index | |
|---|---|
push(array, value) or push(array, value, index)
Adds an element to an array and returns the new array. Has two forms: - push(array, value) - Appends value to the end - push(array, value, index) - Inserts value at the specified index
Parameters: - array: An array. - value: The element to add. - index (optional): The index where to insert the element.
Returns: - (array, error): The new array.
Example:
| Append Element | |
|---|---|
pop(array, index)
Removes an element at a specific index from an array and returns the new array and the removed element.
Parameters: - array: An array. - index: The index of the element to remove (integer).
Returns: - On success: (array, any, None): The new array, the removed element, and no error. - On error: (None, error): None and the error message.
Note: This function has inconsistent return values - success returns 3 values, error returns 2 values.
Example:
replace(array, index, value)
Replaces an element at a specific index in an array.
Parameters: - array: An array. - index: The index of the element to replace. - value: The new value.
Returns: - (array, error): The modified array.
Example:
| Replace Element | |
|---|---|
delete(array, index)
Removes an element at a specific index in an array.
Parameters: - array: An array. - index: The index of the element to remove.
Returns: - (array, error): The modified array.
Example:
| Remove at Index | |
|---|---|
copy(array)
Creates a shallow copy of an array.
Parameters: - array: An array.
Returns: - (array, error): A new array that is a copy of the original.
Example:
| Copy Array | |
|---|---|
clear(array)
Removes all elements from an array.
Parameters: - array: An array.
Returns: - (array, error): The empty array.
Example:
| Clear Array | |
|---|---|
equal(array1, array2)
Compares two arrays for equality.
Parameters: - array1: The first array. - array2: The second array.
Returns: - (boolean, error): true if the arrays are equal, false otherwise.
Example:
| Compare Arrays | |
|---|---|
sort(array, order)
Sorts the elements of an array according to the specified order. Currently supports integers only.
Parameters: - array: An array of integers (mixed arrays will cause an error). - order: A string specifying sort order - "asc", "ascending", "desc", or "descending".
Returns: - (array, error): A new sorted array, or error if array contains mixed types.
Example:
memory(array)
Returns the memory usage of an array.
Parameters: - array: An array.
Returns: - (integer, error): The memory usage in bytes.
Example:
| Memory Usage | |
|---|---|
tojson(array)
Converts an array to a JSON string.
Parameters: - array: An array.
Returns: - (string, error): The JSON string representation of the array.
Example:
| Convert to JSON | |
|---|---|
tostring(array)
Converts an array to a string representation.
Parameters: - array: An array.
Returns: - (string, error): The string representation of the array.
Example:
| Convert to String | |
|---|---|
Sorting
Harneet supports non-mutating array sorting with natural order and custom comparators. You can access these via the arrays module aliases shown below. These aliases delegate to the global built-ins under the hood.
arrays.SortFn(array)
Returns a new array sorted in natural order.
Behavior: - Numbers are sorted in ascending order. - Strings are sorted lexicographically. - Errors on mixed element types or unsupported element types. - Non-mutating: the original array is not changed. - Returns a tuple: (array, error).
Example:
arrays.SortBy(array, less function(T, T) bool)
Returns a new array sorted using a user-supplied comparator function. The sort is stable.
Behavior: - Comparator signature must match the array element type: function(T, T) bool. - Returns a tuple: (array, error). - Non-mutating: the original array is not changed. - Stable sorting preserves the relative order of equal elements.
Examples:
Slicing
Harneet supports Go-like slicing on arrays, typed arrays, and strings using the s[a:b] syntax. Four forms are available:
s[a:b]s[:b]s[a:]s[:]
Rules and semantics
- Sliceable types: arrays, typed arrays, and strings.
- Indices:
aandbmust be integers. The type checker validates this at compile time. - Bounds: indices are clamped to the valid range
[0, len(s)]. - Order: if
a > b, the result is an empty slice[]. - Half-open:
ais inclusive,bis exclusive. - Typed arrays: slicing preserves the array's element type (e.g.,
int[]). - Strings: result is a
string. Slicing is currently byte-based.
Examples
Notes on strings:
- Current implementation slices by byte index. If your strings contain multi-byte UTF-8 characters, ensure indices align with character boundaries to avoid splitting a character. Rune-aware slicing may be introduced in the future.