findLast<T> function

Option<T> Function(Predicate<T> predicate) findLast<T>(
  1. ImmutableList<T> list
)

Returns a function that searches for the last 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 findLastEven = findLast<int>(myList);
var itemOption = findLastEven((item) => item % 2 == 0); // Returns option containing value 4
  • 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 last element that satisfies the given predicate, or None<T>() if no such element is found.

Implementation

option.Option<T> Function(Predicate<T> predicate) findLast<T>(
    ImmutableList<T> list) {
  return (Predicate<T> predicate) {
    for (T item in list.items.reversed) {
      if (predicate(item)) {
        return option.of(item);
      }
    }
    return option.None<T>();
  };
}