Skip to main content

Mojo function

repeat

repeat[ElementType: Copyable & Movable](element: ElementType, *, times: Int) -> _RepeatIterator[ElementType]

Constructs an iterator that repeats the given element a specified number of times.

This function creates an iterator that returns the same element over and over for the specified number of times.

Examples:

# Repeat a value 3 times
var it = repeat(42, times=3)
for val in it:
    print(val)  # Prints: 42, 42, 42

# Repeat a string 5 times
var str_it = repeat("hello", times=5)
for s in str_it:
    print(s)  # Prints: hello, hello, hello, hello, hello

Parameters:

  • โ€‹ElementType (Copyable & Movable): The type of the element to repeat.

Args:

  • โ€‹element (ElementType): The element to repeat.
  • โ€‹times (Int): The number of times to repeat the element.

Returns:

_RepeatIterator: An iterator that repeats the element the specified number of times.

Was this page helpful?