Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

julia iteration : start, next, done side effects

Tags:

julia

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.

like image 990
Issam T. Avatar asked Dec 24 '22 02:12

Issam T.


1 Answers

If possible, those functions are not supposed to mutate the iterator (such that the iterator state can be copied and re-used). However, there are some prominent examples where such a design is not possible, or only possible with significant performance penalties. The prime example of this is Base.Task, which is iterable (each iteration running until the next produce statement):

julia> collect(@async for i = 1:10
       produce(i)
       end)
10-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10

In those cases we've generally been fine with mutation (though of course any use of the iteration protocol that caches states won't work). The exclamation point at the end of functions is a convention, but is not enforced in any way (and is not strictly limited to mutation of its input arguments, but rather having some side effect that you want to make sure the programmer is aware of).

like image 67
Keno Fischer Avatar answered Dec 30 '22 20:12

Keno Fischer