Rewind iterators

Hello,

I wonder if it’s possible to rewind iterator (ie without) recreating it.

I tried

julia> itr = Iterators.Stateful([10,11,12])
Base.Iterators.Stateful{Array{Int64,1},Union{Nothing, Tuple{Int64,Int64}}}([10, 11, 12], (10, 2), 0)

julia> collect(itr)
3-element Array{Int64,1}:
 10
 11
 12

julia> collect(itr)
0-element Array{Int64,1}

julia> rewind(itr)
ERROR: UndefVarError: rewind not defined
Stacktrace:
 [1] top-level scope at REPL[28]:1

Any idea?

It depends on what you will do next. the thing is, when you collect, you allocate anyways. If you need your itr to survive multiple action, you can use rest
https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.rest

1 Like

Well…

I really think that such a rewind function shouldn’t be difficult to implement.

julia> itr = Iterators.Stateful([10,11,12])
Base.Iterators.Stateful{Array{Int64,1},Union{Nothing, Tuple{Int64,Int64}}}([10, 11, 12], (10, 2), 0)

julia> collect(itr)
3-element Array{Int64,1}:
 10
 11
 12

julia> collect(itr)
0-element Array{Int64,1}

julia> rewind(itr::Iterators.Stateful) = Iterators.Stateful(itr.itr)
rewind (generic function with 1 method)

julia> itr = rewind(itr)
Base.Iterators.Stateful{Array{Int64,1},Union{Nothing, Tuple{Int64,Int64}}}([10, 11, 12], (10, 2), 0)

julia> collect(itr)
3-element Array{Int64,1}:
 10
 11
 12

I wonder if an inplace rewind function (named for example rewind!) couldn’t be implemented

Your opinion?

Rewinding iterators in general is impossible. For example, consider:

proc = open(`sh -c 'while true; do echo $RANDOM; done'`)
iter = eachline(proc)

For stateless iterators, you can use IterTools.peekiter. It should be possible to implement this for stateful iterators (or iterators that perform expensive some computation) by buffering peeked elements.

1 Like

you are basically recreating your itr, what (from an efficiency perspective) matters is the relation between the data (i.e itr and its elements), can you maybe construct a use case for this and see what’s the best options are?

1 Like