# Generator questions

**URL:** https://discourse.julialang.org/t/generator-questions/73665
**Category:** General Usage
**Tags:** generator
**Created:** [December 27, 2021, 12:25am UTC](https://discourse.julialang.org/t/generator-questions/73665 "2021-12-27T00:25:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 27, 2021, 12:25am UTC](https://discourse.julialang.org/t/generator-questions/73665/1 "2021-12-27T00:25:04Z")

</div>

While poking around with the generator example below to try understanding the concept, some questions popped up:

- Why is the last equality false given that the generator was regenerated?
- With the command `g.f(g.iter[n])`, are we materializing the nᵗʰ element of the generator?

```julia
A = [1 2; 3 4]
B = [A^i for i in 0:2]

g = (A*b for b in B) # creates generator
G = collect(g) # 3-element Vector{Matrix{Int64}}
g.f(g.iter[2]) == G[2] # true
g.f(g.iter[3]) == G[3] # true

g.iter[3] = ones(2,2) # assigned to a different matrix here
g.f(g.iter[3]) == G[3] # false (OK)
g = (A*b for b in B) # re-creates generator
g.f(g.iter[2]) == G[2] # true
g.f(g.iter[3]) == G[3] # false (why does the previous assignment still stand?)

```

Thank you.

---

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [December 27, 2021, 2:15am UTC](https://discourse.julialang.org/t/generator-questions/73665/2 "2021-12-27T02:15:38Z")

</div>

> Why is the last equality false given that the generator was regenerated?

As there is no copy for `B`. The generator `g` holds the original reference of the iterator here:

```julia
julia> objectid(g.iter)
0x35595f9c567dff49

julia> objectid(B)
0x35595f9c567dff49

```

> With the command `g.f(g.iter[n])` , are we materializing the nᵗʰ element of the generator?

Only for `(... for element in array)` it can be `true`, but **in general it is not the case**.

There are two counterexamples to your guess.

1. not all generators use the notion of “nᵗʰ element”:

```julia
julia> res = ( [i, j] for i = 1: 2, j = 1:2)
julia> n = 1; res.iter[1]

ERROR: MethodError: no method matching getindex(::Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}, ::Int64)

```

1. we use the [iterator interface](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration) to handle iterations. Random accessing is not available for iterables.

```julia
julia> using DataStructures
julia> xs = list(1, 2, 3)
list(1, 2, 3)

julia> g = (k * 2 for k in xs)
Base.Generator{Cons{Int64}, var"#17#18"}(var"#17#18"(), list(1, 2, 3))

julia> collect(g)
3-element Vector{Int64}:
 2
 4
 6

g.iter[1]
ERROR: MethodError: no method matching getindex(::Cons{Int64}, ::Int64)

```
