Iteration in Julia can be achieved for a new defined type by implementing the iteration interface that has the 3 functions: start, next, done
I see no exclamation point on the end of those functions so from my understanding of julia naming conventions these 3 functions should not modify their arguments. In particular these two loops should give the same output
state = start(iter)
while !done(iter, state)
(i, state) = next(iter, state)
@show i
end
state = start(iter)
while !done(iter, state)
(other_i, other_state) = next(iter, state)
(i, state) = next(iter, state)
@show i
end
Am I wrong? I ask because I bumped into some iterators in external julia packages not respecting this.
state, I suppose there's no!in the name because even though in theory thenextfunction could mutate the receivedstateobject and simply return it again, rather than just returning a new state object, in practice thestateobject is not supposed to be manipulated directly, but is meant to be internal to theforstatement, as isnext. So presumably lack of ! is just for consistency. - Tasos Papastylianoustatevariable isn't a big deal (might even be a more efficient way of implementing an iterator). I was just curious where you saw it so I could have a look :D - Tasos Papastylianou