For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
filter
def filter[origin: ImmOrigin, IterableType: Iterable, //, Function: def(IterableType.IteratorType[origin].Element) -> Bool](ref[origin] iterable: IterableType, var function: Function) -> _FilterIterator[Function] where conforms_to(IterableType.IteratorType[origin].Element, Deinitable)
Returns an iterator that yields the elements of the input iterable for which function returns True.
Examples:
var l = [1, 2, 3, 4]
# outputs:
# 2
# 4
for elem in filter(l, lambda (x: Int) -> Bool: x % 2 == 0):
print(elem)
Parameters:
- origin (
ImmOrigin): The origin of the iterable. - IterableType (
Iterable): The type of the iterable. - Function (
def(IterableType.IteratorType[origin].Element) -> Bool): The type of the predicate.
Args:
- iterable (
IterableType): The iterable to filter. - function (
Function): The predicate that decides whether to keep each element.
Returns:
_FilterIterator[Function]: A filter iterator that yields the elements function accepts.
def filter[IterableType: IterableOwned, //, Function: def(IterableType.IteratorOwnedType.Element) -> Bool](var iterable: IterableType, var function: Function) -> _FilterIterator[Function] where conforms_to(IterableType.IteratorOwnedType.Element, Deinitable)
Returns an iterator that yields the elements of the input iterable for which function returns True, consuming the iterable.
Parameters:
- IterableType (
IterableOwned): The type of the iterable. - Function (
def(IterableType.IteratorOwnedType.Element) -> Bool): The type of the predicate.
Args:
- iterable (
IterableType): The iterable to consume and filter. - function (
Function): The predicate that decides whether to keep each element.
Returns:
_FilterIterator[Function]: A filter iterator that yields the elements function accepts.