JuliaDynamics / ResumableFunctions.jl

C# style generators a.k.a. semi-coroutines for Julia.
Other
157 stars 18 forks source link

`continue` results in infinite loops #53

Closed ghost closed 3 years ago

ghost commented 3 years ago

The following hangs:

julia> using ResumableFunctions

julia> @resumable function f()
           for i in 1:10
               i % 2 == 0 && continue
               @yield i
           end
       end
f (generic function with 1 method)

julia> collect(f())

Adding a line to print out i shows that the loop goes through once with i = 1, then repeats the loop body forever with i = 2.

hdavid16 commented 3 years ago

Hi @cpsquonk, I have had the same issue. What I ended up doing was using an if statement instead of the continue:

@resumable function f()
    for i in 1:10
        if i % 2 != 0
            @yield i
        end
    end
end
BenLauwens commented 3 years ago

Hi

The for iterator loop is transformed into a while loop. The update of the iterator is done just before the end keyword of the loop ... so continue skips the update and the iterator does not advance... I suppose that continue is replaced by a local jump. Something similar has to be implemented. I keep you informed of possible progress

Kind regards

Ben

BenLauwens commented 3 years ago

I had a really productive evening;) This should work now! Feedback is welcome!