rest

rest(iter, state)

An iterator that yields the same elements as iter, but starting at the given state.

Examples

  1. Generate the rest of an iterator starting from a given state:

    julia> iter = Iterators.cycle([1, 2, 3]);
    julia> rest_iter = rest(iter, 2);
    julia> collect(take(rest_iter, 5))
    5-element Array{Int64,1}:
    2
    3
    1
    2
    3

    In this example, we create an iterator iter that cycles through the elements [1, 2, 3]. We then generate a new iterator rest_iter using rest, starting from the state 2. We collect the first 5 elements from rest_iter using take and store them in an array.

  2. Start from a different state in a range:

    julia> range_iter = Iterators.range(1, step=2, length=10);
    julia> rest_iter = rest(range_iter, 3);
    julia> collect(take(rest_iter, 5))
    5-element Array{Int64,1}:
    5
    7
    9
    11
    13

    In this example, we create a range iterator range_iter starting from 1, with a step of 2, and a length of 10. We generate a new iterator rest_iter using rest, starting from the state 3. We collect the first 5 elements from rest_iter using take and store them in an array.

  3. Use with custom iterator types:
    julia> struct MyIterator
              state::Int
          end
    julia> Base.iterate(itr::MyIterator, state) = state > 0 ? (state, state-1) : nothing
    julia> iter = MyIterator(5);
    julia> rest_iter = rest(iter, 2);
    julia> collect(take(rest_iter, 5))
    5-element Array{Int64,1}:
    2
    1
    0

    Here, we define a custom iterator type MyIterator that counts down from a given state. We implement the iterate method for this iterator. We create an instance of MyIterator with an initial state of 5. We then generate a new iterator rest_iter using rest, starting from the state 2. We collect the first 5 elements from rest_iter using take and store them in an array.

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: