Array

The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.

Katxupa is here just to extend the standard library by adding more utility functions to the Array type. Besides Array standard functions, the library provides the following functions:

Global Functions

listOf

Creates an immutable list from the provided elements.

const immutableList = listOf(1, 2, 3);
// immutableList is [1, 2, 3]

mutableListOf

Creates a mutable list from the provided elements.

const mutableList = mutableListOf(1, 2, 3);
// mutableList is [1, 2, 3]

emptyList

Creates an empty list. It returns an empty array, which represents the list.

const emptyArray = emptyList();
console.log(emptyArray); // []

Class Functions

getOrElse

Gets the element at the specified index or provides a default value if the index is out of bounds.

getOrEmpty

Gets an optional containing the element at the specified index.

associateWith

Associates each element with a key-value pair based on the provided selectors.

sortBy

Sorts the array using the provided comparator function.

plus

Concatenates the array with another array.

minus

Removes elements from the collection that are present in another array.

minusAssign

Appends elements from another array to the collection (mutates the collection).

plusAssign

Appends elements from another array to the collection (mutates the collection).

count

Returns the number of elements in the collection.

removeAll

Removes elements from the collection based on a predicate or a collection of elements.

retainAll

Retains only the elements in the collection that are present in another array.

first

Returns the first element in the collection.

last

Returns the last element in the array.

shuffle

Shuffles the elements in the array randomly.

Last updated