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).
map
def map[origin: ImmOrigin, IterableType: Iterable, ResultType: Copyable, //, Function: def(var IterableType.IteratorType[origin].Element) -> ResultType](ref[origin] iterable: IterableType, var function: Function) -> _MapIterator[Function]
Returns an iterator that applies function to each element of the input iterable.
Examples:
var l = [1, 2, 3]
var m = map(l, lambda (x: Int) -> Int: x + 1)
# outputs:
# 2
# 3
# 4
for elem in m:
print(elem)
Parameters:
- origin (
ImmOrigin): The origin of the iterable. - IterableType (
Iterable): The type of the iterable. - ResultType (
Copyable): The return type of the function. - Function (
def(var IterableType.IteratorType[origin].Element) -> ResultType): The type of the function to apply to each element.
Args:
- iterable (
IterableType): The iterable to map over. - function (
Function): The function to apply to each element.
Returns:
_MapIterator[Function]: A map iterator that yields the results of applying function to each
element.
def map[IterableType: IterableOwned, ResultType: Copyable, //, Function: def(var IterableType.IteratorOwnedType.Element) -> ResultType](var iterable: IterableType, var function: Function) -> _MapIterator[Function]
Returns an iterator that applies function to each element of the input iterable, consuming the iterable.
Parameters:
- IterableType (
IterableOwned): The type of the iterable. - ResultType (
Copyable): The return type of the function. - Function (
def(var IterableType.IteratorOwnedType.Element) -> ResultType): The type of the function to apply to each element.
Args:
- iterable (
IterableType): The iterable to consume and map over. - function (
Function): The function to apply to each element.
Returns:
_MapIterator[Function]: A map iterator that yields the results of applying function to each
element.