# 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:** 1
**Showing post:** 3

<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)) 

```

---

_[View the full topic](https://discourse.julialang.org/t/custom-iterator-with-one-big-computation-at-the-begin-in-place-operation-at-every-iterations/100045)._
