Inverting default `collect` behavior when iterator returns an array

Would something like this work?

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.

2 Likes