findFirst<T> function
- ImmutableList<
T> list
Returns a function that searches for the first element in the
given list
that satisfies the provided Predicate.
If an element is found that satisfies the Predicate, that element
is returned wrapped in an Option<T>
. If no element is found,
None<T>()
is returned.
Usage:
var myList = ImmutableList([1, 2, 3, 4, 5]);
var findFirstEven = findFirst<int>(myList);
var itemOption = findFirstEven((item) => item % 2 == 0); // Returns option containing value 2
- Parameter
list
: The list in which to search for an element. - Returns: A function that takes a Predicate and returns an
Option<T>
containing the first element that satisfies the given predicate, orNone<T>()
if no such element is found.
Implementation
option.Option<T> Function(Predicate<T> predicate) findFirst<T>(
ImmutableList<T> list) {
return (Predicate<T> predicate) {
for (T item in list.items) {
if (predicate(item)) {
return option.of(item);
}
}
return option.None<T>();
};
}