# Inverting default \`collect\` behavior when iterator returns an array

**URL:** <https://discourse.julialang.org/t/inverting-default-collect-behavior-when-iterator-returns-an-array/85409>\
**Category:** General Usage\
**Tags:** iterators\
**Created:** [August 6, 2022, 7:06pm UTC](https://discourse.julialang.org/t/inverting-default-collect-behavior-when-iterator-returns-an-array/85409 "2022-08-06T19:06:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [August 6, 2022, 7:06pm UTC](https://discourse.julialang.org/t/inverting-default-collect-behavior-when-iterator-returns-an-array/85409/1 "2022-08-06T19:06:30Z")

</div>

A MWE:

```julia
struct MyIter
    n
    limit
end

function Base.iterate(m::MyIter)
    state = ones(m.n)
    return (state,state .+ 1)
end

function Base.iterate(m::MyIter,state)
    if first(state) > m.limit
        return nothing
    else
        new_state = state .+ 1
        return (state,new_state)
    end

end

function Base.length(m::MyIter)
    return m.limit
end

collect(MyIter(2,10))

```

This will return:

```julia-repl
julia> collect(MyIter(2,10))
10-element Vector{Any}:
 [1.0, 1.0]
 [2.0, 2.0]
 [3.0, 3.0]
 [4.0, 4.0]
 [5.0, 5.0]
 [6.0, 6.0]
 [7.0, 7.0]
 [8.0, 8.0]
 [9.0, 9.0]
 [10.0, 10.0]

```

Is there a different design such that my iterator could return `[[1.0,2.0...,10.0],[1.0,2.0...,10.0]]` or an `n` by `limit`matrix, without users needing to do that step manually when `collect`ing?

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [August 6, 2022, 7:56pm UTC](https://discourse.julialang.org/t/inverting-default-collect-behavior-when-iterator-returns-an-array/85409/2 "2022-08-06T19:56:20Z")

</div>

Would something like this work?

```julia
struct MyIter
    n
    limit
end

function Base.iterate(m::MyIter)
    state = ones(m.limit)
    for i in 1:m.limit
         state[i] = i
    end
    return (state, (state,1))
end

function Base.iterate(m::MyIter,state)
    if state[2] > m.n
        return nothing
    else
        return (state[1], state[2]+1)
    end

end

function Base.length(m::MyIter)
    return m.n
end

```

I’m on mobile on the road so I can’t test it right now, but I think this will return the data in the flipped orientation.

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [August 6, 2022, 8:13pm UTC](https://discourse.julialang.org/t/inverting-default-collect-behavior-when-iterator-returns-an-array/85409/3 "2022-08-06T20:13:06Z")

</div>

Just realized that this will return arrays that all point to the same object. The second iterate function should return a copy of `state[1]` if you want all of the numbers to operate independently.

---

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [August 7, 2022, 3:56am UTC](https://discourse.julialang.org/t/inverting-default-collect-behavior-when-iterator-returns-an-array/85409/4 "2022-08-07T03:56:54Z")

</div>

Thank you - I took your essential idea of iterating over each item, therefore effectively collecting column by column instead of row by row.
