# Rewind iterators

**URL:** https://discourse.julialang.org/t/rewind-iterators/30930
**Category:** General Usage
**Created:** [November 10, 2019, 7:43pm UTC](https://discourse.julialang.org/t/rewind-iterators/30930 "2019-11-10T19:43:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)
#### Post date: [November 10, 2019, 7:43pm UTC](https://discourse.julialang.org/t/rewind-iterators/30930/1 "2019-11-10T19:43:04Z")

</div>

Hello,

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

I tried

```julia
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?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [November 10, 2019, 7:50pm UTC](https://discourse.julialang.org/t/rewind-iterators/30930/2 "2019-11-10T19:50:44Z")

</div>

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](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.rest)

---

<div class="post-metadata">

### Author: ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)
#### Post date: [November 10, 2019, 8:37pm UTC](https://discourse.julialang.org/t/rewind-iterators/30930/3 "2019-11-10T20:37:55Z")

</div>

Well…

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

```julia
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?

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [November 10, 2019, 9:05pm UTC](https://discourse.julialang.org/t/rewind-iterators/30930/4 "2019-11-10T21:05:15Z")

</div>

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

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

```

For _stateless_ iterators, you can use [`IterTools.peekiter`](https://juliacollections.github.io/IterTools.jl/latest/#IterTools.peekiter). It should be possible to implement this for stateful iterators (or iterators that perform expensive some computation) by buffering peeked elements.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [November 10, 2019, 11:06pm UTC](https://discourse.julialang.org/t/rewind-iterators/30930/5 "2019-11-10T23:06:08Z")

</div>

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?
