list
The list package adds predicate helpers and searches to lists. Because the package extends the list type, most functions can also be called as methods:
nums = [3, 8, 1, 8, 5]
println nums.reject(do (n) => n > 4) # [3, 1]println nums.first_where(do (n) => n > 4) # 8println nums.last_where(do (n) => n > 4) # 5println nums.one(do (n) => n == 1) # trueprintln nums.is_unique() # falseprintln nums.index_of(8) # 1println nums.last_index_of(8) # 3println [1, 2.5, "x", 3].sum_integer() # 4A few functions share their names with built-in methods or type keywords. The built-in wins when called as a method, so call these with the package prefix:
nums = [3, 8, 1, 8, 5]
println list::any(nums, do (n) => n > 7) # trueprintln list::none(nums, do (n) => n > 9) # trueprintln list::all(nums, do (n) => n > 0) # trueprintln list::count(nums, do (n) => n == 8) # 2println list::find(nums, do (n) => n == 8) # 1 (the index)list::iterator(xs), or xs.iterator(), returns a ListIterator.
Functions
Section titled “Functions”list::iterator
Section titled “list::iterator”list::iterator(_list: list)Creates an iterator over a list.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to iterate over. |
Returns object — A ListIterator instance.
list::all
Section titled “list::all”list::all(_list: list, _predicate: lambda): booleanReturns true if every element in the list satisfies the predicate.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to test. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns boolean — true if all elements satisfy the predicate, false otherwise.
list::any
Section titled “list::any”list::any(_list: list, _predicate: lambda): booleanReturns true if any element in the list satisfies the predicate.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to test. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns boolean — true if at least one element satisfies the predicate.
list::count
Section titled “list::count”list::count(_list: list, _predicate: lambda): integerReturns the count of elements satisfying the predicate.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to test. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns integer — The number of matching elements.
list::find
Section titled “list::find”list::find(_list: list, _predicate: lambda): integerFinds the index of the first element satisfying the predicate.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to search. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns integer — The index of the first matching element, or -1 if not found.
list::first_where
Section titled “list::first_where”list::first_where(_list: list, _predicate: lambda): anyReturns the first element satisfying the predicate, or null.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to search. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns any — The first matching element, or null if none found.
list::index_of
Section titled “list::index_of”list::index_of(_list: list, _value: any): integerReturns the first index of a given value in the list.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to search. | |
_value | any | The value to find. |
Returns integer — The index of the first occurrence, or -1 if not found.
list::last_index_of
Section titled “list::last_index_of”list::last_index_of(_list: list, _value: any): integerReturns the last index of a given value in the list.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to search. | |
_value | any | The value to find. |
Returns integer — The index of the last occurrence, or -1 if not found.
list::is_unique
Section titled “list::is_unique”list::is_unique(_list: list): booleanReturns true if every element in the list is unique.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to check. |
Returns boolean — true if all elements are distinct.
list::last_where
Section titled “list::last_where”list::last_where(_list: list, _predicate: lambda): anyReturns the last element satisfying the predicate, or null.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to search. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns any — The last matching element, or null if none found.
list::none
Section titled “list::none”list::none(_list: list, _predicate: lambda): booleanReturns true if no element satisfies the predicate.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to test. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns boolean — true if no elements satisfy the predicate.
list::one
Section titled “list::one”list::one(_list: list, _predicate: lambda): booleanReturns true if exactly one element satisfies the predicate.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The list to test. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns boolean — true if exactly one element matches.
list::reject
Section titled “list::reject”list::reject(_list: list, _predicate: lambda): listReturns a list of elements that do not satisfy the predicate.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | The source list. | |
_predicate | lambda | A single-argument lambda returning a boolean. |
Returns list — A new list of elements for which the predicate returned false.
list::sum_integer
Section titled “list::sum_integer”list::sum_integer(_list: list): integerComputes the integer sum of all integer elements in the list.
| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list | A list of mixed values (non-integer values are discarded). |
Returns integer — The sum of all integer elements.
list::sum_float
Section titled “list::sum_float”list::sum_float(_list: list): float| Parameter | Type | Default | Description |
|---|---|---|---|
_list | list |
Returns float