findIndex<T> function
- Predicate<
T> predicate
Returns a function that takes an ImmutableList<T>
and searches
for the index of the first element that satisfies the given predicate
.
If an element is found that satisfies the predicate
, the index of
that element is returned wrapped in an Option<int>
.
If no element is found, None<int>()
is returned.
Usage:
var myList = ImmutableList([1, 2, 3, 4, 5]);
var findIndexOfFirstEven = findIndex<int>((item) => item % 2 == 0);
var indexOption = findIndexOfFirstEven(myList); // Returns option containing index 1
- Parameter
predicate
: The condition used to test each element. - Returns: A function that processes an
ImmutableList<T>
and returns anOption<int>
containing the index of the first element that satisfies the given predicate, orNone<int>()
if no such element is found.
Implementation
option.Option<int> Function(ImmutableList<T>) findIndex<T>(
Predicate<T> predicate) {
return (ImmutableList<T> list) {
final index = list.items.indexWhere(predicate);
return index != -1 ? option.of(index) : option.None<int>();
};
}