Skip to content

Enumerable

int len ( enumerable value )

Returns the length of value.
For strings, this is the number of characters.
For arrays, this is the number of elements.
For objects, this is the number of key-value pairs.


enumerable delete ( enumerable target, int | string index )

Returns a copy of target with the value at target[index] deleted from it.
For arrays and strings, this is the nth value, where n is index.
For object, this is the value with a key of index.


enumerable set ( enumerable target, int | string index, any value )

Returns a copy of target with the value at target[index] set to value.
For arrays and strings, this is the nth value, where n is index.
For object, this is the value with a key of index.


int index ( string | array target, string | any element )

If target is an array, it returns the index of the first occurence of element in target.
If target is a string, it returns the index of the first character of the first substring to match element.

If target does not contain element, it returns -1.


bool contains? ( string | array target, string | any element )

Returns whether target contains element.


bool any ( enumerable target, proc predicate )

Returns whether predicate, when called on all elements of target separately, returns a truthy value at least once.


bool every ( enumerable target, proc predicate )

Returns whether predicate, when called on all elements of target separately, returns a truthy value every time.


string | array reverse ( string | array target )

Returns a new string or array, where all elements are in reverse order to target.


string | array slice ( string | array target, int offset, int count)

Returns count amount of elements, starting from position offset.
If count is not specified, it returns all objects starting from position offset.


array flat ( array target, int depth = 1 )

Recursively collapses child array elements in target in to the output, to a depth of depth.


array map ( enumerable target, proc proc )

Calls proc for each element in target.
Returns an array of all the return values of proc.


array sort ( array target, proc proc )

Sorts the elements of target, based on the return value of proc, given the compare value as arg 0, and the pivot as arg 1.

The proc should return a negative or zero number if arg 0 is to follow arg 1, or a positive number if arg 1 is to follow arg 0.

This can easily be accomplished with the <=> operator.

If proc is unset, sort will default to sorting by value in ascending order.


enumerable filter ( enumerable target, proc predicate )

Returns target, with only the elements for which predicate, when called with that element, returns a truthy value.


any find ( enumerable target, proc predicate )

Returns the first element of target for which predicate, when called with that element, returns a truthy value.
If target is an object, it returns the value of the key-value pair for with predicate returns truthy.


any reduce ( enumerable target, any value, proc proc )

Reduces an enumerable into a single value.
Calls proc with the next element of target and the previous return value of proc, or value to begin with.
Returns the last return value of proc.


array shuffle ( array target )

Returns a new array containing the elements of target, in a random order.