lastWhile method

Iterable<T> lastWhile(
  1. bool predicate(
    1. T element
    )
)

Yields elements from the end of the iterable as long as the predicate condition is met.

This function iterates over the elements of the iterable in reverse order. For each element, it checks the given predicate function. If the predicate returns true, the element is added to a queue. The iteration stops as soon as the predicate returns false. The elements are then returned in their original order.

Parameters: predicate (Function(T element)): A function that takes an element as input and returns a boolean. If it returns true, the element is included in the result.

Returns: Iterable

Example:

List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> result = numbers.lastWhile((n) => n > 2); // Yields [3, 4, 5]

Note: The returned iterable is lazy in its reverse iteration, but the elements are stored in a ListQueue to preserve their original order when returned.

Implementation

Iterable<T> lastWhile(bool predicate(T element)) {
  var list = ListQueue<T>();
  for (var element in reversed) {
    if (!predicate(element)) break;
    list.addFirst(element);
  }
  return list;
}