The collection
module
The collection
module is the common super-metatable for all collection types, such as list
, string
, and bytes
. It contains the common operations for all collections.
Member functions
all(self, predicate: callable[any → boolean]): boolean
Returns true
if all elements in the collection return true
when passed to predicate
.
any(self, predicate: callable[any → boolean]): boolean
Returns true
if any element in the collection returns true
when passed to predicate
. predicate
must accept one argument and return a boolean.
filter(self, predicate: callable[any → boolean]): any
Returns a new collection containing only the elements in the collection that return true
when passed to predicate
.
first(self, f: callable[any → boolean] | null = null): any
Returns the first element in the collection that returns true
when passed to f
. If f
is null
, returns the first element in the collection.
getOrDefault(self, i: number, default: any): any
Returns the value at i
if it exists, otherwise returns default
.
isEmpty(self): boolean
Returns true
if the collection is empty.
last(self, f: callable[any → boolean] | null = null): any
Returns the last element in the collection that returns true
when passed to f
. If f
is null
, returns the last element in the collection.
map(self, mapper: callable[any → any]): any
Returns a new collection containing the results of calling mapper
on each element in the collection. mapper
must accept one argument and return a value.
reduce(self, reducer: callable[any, any → any], initial: any): any
Returns the result of calling reducer
on each element in the collection, passing the result of the previous call as the first argument and the current element as the second argument. The first call to reducer
will receive initial
as the first argument and the first element as the second argument. reducer
must accept two arguments and return a value.
sum(self): any
Semantically equivalent to reduce(fn(a, b) = a + b, 0)
.