repeated(x[, n::Int])

An iterator that generates the value x forever. If n is specified, generates x that many times (equivalent to take(repeated(x), n)).


  1. Generate an infinite iterator of a single value:

    julia> it = repeated(10);
    julia> collect(take(it, 5))
    5-element Array{Int64,1}:

    In this example, repeated(10) creates an infinite iterator that generates the value 10 forever. take(it, 5) limits the iterator to generate only 5 values, which are then collected into an array.

  2. Generate a specific number of repeated values:

    julia> collect(repeated("Hello", 3))
    3-element Array{String,1}:

    The repeated function can also generate a specific number of repeated values. In this example, repeated("Hello", 3) generates the string "Hello" three times, which are then collected into an array.

  3. Use repeated with other functions:
    julia> sum(repeated(2, 5))

    repeated can be combined with other functions. Here, repeated(2, 5) generates the value 2 five times, and sum is used to calculate the sum of these repeated values.

Common mistake example:

julia> it = repeated(3, -2)
ERROR: ArgumentError: Number of repetitions must be non-negative

In this example, a negative value is provided for the number of repetitions (n). The documentation clearly states that the number of repetitions should be non-negative, so it's important to ensure that a valid value is used for n when using repeated.

See Also

countfrom, cycle, done, drop, eachindex, enumerate, first, repeated, rest, start, svds, take, vecdot, vecnorm, zip,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: