cycle

cycle(iter)

An iterator that cycles through iter forever.

Examples

  1. Cycle through an array:

    julia> arr = [1, 2, 3];
    julia> cycled = cycle(arr);
    julia> collect(take(cycled, 7))
    7-element Array{Int64,1}:
    1
    2
    3
    1
    2
    3
    1

    This example creates an iterator cycled that cycles through the array arr forever. By using take, we collect the first 7 elements from the iterator.

  2. Cycle through a range:

    julia> rng = 1:3;
    julia> cycled = cycle(rng);
    julia> collect(take(cycled, 6))
    6-element Array{Int64,1}:
    1
    2
    3
    1
    2
    3

    It demonstrates how to cycle through a range using cycle.

  3. Use cycle with strings:
    julia> word = "Julia";
    julia> cycled = cycle(word);
    julia> collect(take(cycled, 9))
    9-element Array{Char,1}:
    'J'
    'u'
    'l'
    'i'
    'a'
    'J'
    'u'
    'l'
    'i'

    This example shows how to cycle through a string.

Common mistake example:

julia> arr = [1, 2, 3];
julia> cycled = cycle(arr);
julia> take(cycled, 0)

In this example, the take function is used with a count of 0. It results in an empty collection because no elements are taken. It's important to provide a positive count value when using take with cycle to retrieve elements.

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
Details

Checking you are not a robot: