JuliaDynamics / ResumableFunctions.jl

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

issue with generators of two variables #21

Closed jverzani closed 6 years ago

jverzani commented 6 years ago

I'm trying out

- ResumableFunctions            0.1.10+            Python-generators-v0.6

And have found an unexpected behavior:

julia> @resumable function g(n)
       for i in 1:n
       @yield i
       end
       end

And

julia> [(a,b) for a in g(2), b in g(3)]
8-element Array{Tuple{Any,Int64},1}:
 (1, 1)      
 (2, 1)      
 (nothing, 2)
 (1, 2)      
 (2, 2)      
 (nothing, 3)
 (1, 3)      
 (2, 3) 

Whereas collecting first returns:

julia> [(a,b) for a in collect(g(2)), b in collect(g(3))]
2×3 Array{Tuple{Int64,Int64},2}:
 (1, 1)  (1, 2)  (1, 3)
 (2, 1)  (2, 2)  (2, 3)

Shouldn't this be the output of the former, or am I missing something?

BenLauwens commented 6 years ago

Apparently, the double iterator protocol has a problem. I presume that this is one of the corner cases I can not solve as long as the julep about iterators is not available. The function done precalculates the next value and the function next simply returns the already calculated value... I has to do this to exclude the default return.

jverzani commented 6 years ago

Okay, thanks for the explanation!