firstWhile method
Yields elements from the iterable as long as the predicate condition is met.
This function iterates over the elements of the iterable. For each element,
it checks the given predicate function. If the predicate returns true,
the element is yielded. The iteration stops as soon as the predicate returns false.
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.firstWhile((n) => n < 4); // Yields [1, 2, 3]
Note: The returned iterable is lazy. Elements are only processed when they are iterated over.
Implementation
Iterable<T> firstWhile(bool predicate(T element)) sync* {
for (var element in this) {
if (!predicate(element)) break;
yield element;
}
}