Skip to main content

Mojo function

map

map[origin: ImmutableOrigin, IterableType: Iterable, ResultType: Copyable & Movable, //, function: fn(var IterableType.IteratorType[False, origin].Element) -> ResultType](ref [origin] iterable: IterableType) -> _MapIterator[function]

Returns an iterator that applies function to each element of the input iterable.

Examples:

var l = [1, 2, 3]
fn add_one(x: Int) -> Int:
    return x + 1
var m = map[add_one](l)

# outputs:
# 2
# 3
# 4
for elem in m:
    print(elem)

Parameters:

  • origin (ImmutableOrigin): The origin of the iterable.
  • IterableType (Iterable): The type of the iterable.
  • ResultType (Copyable & Movable): The return type of the function.
  • function (fn(var IterableType.IteratorType[False, origin].Element) -> ResultType): The function to apply to each element.

Args:

  • iterable (IterableType): The iterable to map over.

Returns:

_MapIterator: A map iterator that yields the results of applying function to each element.

Was this page helpful?