IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0
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[origins: OriginSet, //, func: def(Int) capturing thin -> None](size: Int)

Maps a function over the integer range [0, size). This lets you apply an integer index-based operation across data captured by the mapped function (for example, an indexed buffer).

For example:

from std.algorithm import map

def main() raises:
# Create list with initial values to act on
var list: List[Float32] = [1.0, 2.0, 3.0, 4.0, 5.0]

# Function applied to the value at each index
@parameter
def exponent_2(idx: Int):
list[idx] = 2.0 ** list[idx]

# Apply the mapped function across the index range
map[exponent_2](len(list))

# Show results
for idx in range(len(list)):
print(list[idx])

Example output:

2.0
4.0
8.0
16.0
32.0

Parameters:

  • origins (OriginSet): Capture origins for mapped function.
  • func (def(Int) capturing thin -> None): Parameterized function applied at each index.

Args:

  • size (Int): Number of elements in the index range.