rest
rest(iter, state)
An iterator that yields the same elements as iter
, but starting at the given state
.
Examples
-
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 iteratorrest_iter
usingrest
, starting from the state2
. We collect the first 5 elements fromrest_iter
usingtake
and store them in an array. -
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 iteratorrest_iter
usingrest
, starting from the state3
. We collect the first 5 elements fromrest_iter
usingtake
and store them in an array. - 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 theiterate
method for this iterator. We create an instance ofMyIterator
with an initial state of5
. We then generate a new iteratorrest_iter
usingrest
, starting from the state2
. We collect the first 5 elements fromrest_iter
usingtake
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.