# Custom Iterator with one big computation at the begin + in place operation at every iterations

**URL:** <https://discourse.julialang.org/t/custom-iterator-with-one-big-computation-at-the-begin-in-place-operation-at-every-iterations/100045>\
**Category:** General Usage\
**Tags:** iterators\
**Created:** [June 8, 2023, 9:27am UTC](https://discourse.julialang.org/t/custom-iterator-with-one-big-computation-at-the-begin-in-place-operation-at-every-iterations/100045 "2023-06-08T09:27:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [June 8, 2023, 9:27am UTC](https://discourse.julialang.org/t/custom-iterator-with-one-big-computation-at-the-begin-in-place-operation-at-every-iterations/100045/1 "2023-06-08T09:27:16Z")

</div>

I am trying to build a custom iterator for the `QuasiMonteCarlo.jl` package.

I did not find an example doing the kind of things I want.

1. I need first to generate 3 big matrices

`for 1:M`

1. “randomize” in place these matrices

2. compute something with the result and store the result

`end`

I don’t know how to build an iterator that store something and then use it at every iteration.

Here is a very simplified version of the problematic

```julia
# Iteration 0 -> create a matrix

N = d*n

X = reshape(range(0, step = π, length = N) .% 1, d, n)

# Iteration 1:M -> do something in place on the matrix

shuffle!(X)

```

Ideally, once the iterator is defined, application would look like

```julia
mean(X for MyIterator(M, args))

```

From the [docs on iterators](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration) I cannot figure out, how to store and use an `X` in the iterator.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 9, 2023, 11:34am UTC](https://discourse.julialang.org/t/custom-iterator-with-one-big-computation-at-the-begin-in-place-operation-at-every-iterations/100045/2 "2023-06-09T11:34:37Z")

</div>

> [@dmetivie](#):
>
> I don’t know how to build an iterator that store something and then use it at every iteration.

Define a data structure (a `struct`) that stores whatever you need, then define an `iterate` method for it.

For example, the [Sobol.jl package](https://github.com/JuliaMath/Sobol.jl) defines a `SobolSeq` data structure, and then has an `iterate` method for it.

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [June 13, 2023, 11:44am UTC](https://discourse.julialang.org/t/custom-iterator-with-one-big-computation-at-the-begin-in-place-operation-at-every-iterations/100045/3 "2023-06-13T11:44:15Z")

</div>

Thanks!  
I used your piece of code + [that](https://discourse.julialang.org/t/proper-definition-of-eltype-for-custom-iterator/21903/2) which looks simpler (and maybe more up to date according to docs?).

```julia
using Random
struct MyIterator{T <: Real}
    X::Matrix{T}
    count::Int
end

MyIterator(n::Integer, d::Integer, M::Integer) = MyIterator(reshape(range(0, step = π, length = d*n), d, n).%1, M)

Base.length(s::MyIterator) = s.count
Base.iterate(s::MyIterator{T}, state=1) where {T} = state > s.count ? nothing : (shuffle!(@view(s.X[:,:])), state+1) # view for in place modification of s.X
Base.eltype(::Type{MyIterator{T}}) where {T} = Matrix{T}

# it works
for i in MyIterator(5,2,3) println(i) end
collect(MyIterator(5,2,3)) 

```
