Mojo function
drop_while
drop_while[origin: ImmutOrigin, IterableType: Iterable, //, predicate: def(IterableType.IteratorType[False, origin._mlir_origin, origin].Element) -> Bool](ref[origin] iterable: IterableType) -> _DropWhileIterator[predicate] where conforms_to(IterableType.IteratorType[False, origin._mlir_origin, origin].Element, AnyType & ImplicitlyDestructible)
Creates an iterator that drops elements while predicate returns True.
This function returns an iterator that drops elements from the input iterable as long as the predicate function returns True for each element. Once the predicate returns False, the iterator starts yielding all remaining elements unconditionally.
Examples:
from std.itertools import drop_while
# Drop while less than 5
def less_than_5(x: Int) -> Bool:
return x < 5
var nums = [1, 2, 3, 4, 5, 6, 1, 2]
for num in drop_while[less_than_5](nums):
print(num) # Prints: 5, 6, 1, 2Parameters:
- origin (
ImmutOrigin): The origin of the iterable. - IterableType (
Iterable): The type of the iterable. - predicate (
def(IterableType.IteratorType[False, origin._mlir_origin, origin].Element) -> Bool): A function that takes an element and returns True if the element should be dropped.
Args:
- iterable (
IterableType): The iterable to drop elements from.
Returns:
_DropWhileIterator: An iterator that drops elements while predicate returns True, then
yields all remaining elements.
drop_while[IterableType: IterableOwned, //, predicate: def(IterableType.IteratorOwnedType.Element) -> Bool](var iterable: IterableType) -> _DropWhileIterator[predicate] where conforms_to(IterableType.IteratorOwnedType.Element, AnyType & ImplicitlyDestructible)
Creates an iterator that drops elements while predicate returns True, consuming the iterable.
Parameters:
- IterableType (
IterableOwned): The type of the iterable. - predicate (
def(IterableType.IteratorOwnedType.Element) -> Bool): A function that takes an element and returns True if the element should be dropped.
Args:
- iterable (
IterableType): The iterable to consume and drop elements from.
Returns:
_DropWhileIterator: An iterator that drops elements while predicate returns True, then
yields all remaining elements.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!