Generator Evaluation Order?

julia> fun() = begin
           res = 0
           map(tuple,
               begin
                       ret = ((a=1/(i+j);res+=a;println("VAL = ",res);a) for i=1:2, j=1:2)
                       println("RES : ",res)
                       ret
               end,
           [1 3; 2 4])
           res
       end
fun (generic function with 1 method)

julia> fun()
RES : 0
VAL = 0.5
VAL = 0.8333333333333333
VAL = 1.1666666666666665
VAL = 1.4166666666666665
1.4166666666666665

why later coming println statement is printed out first? Why is the evaluation order not preserved?

Thanks

ret = ... just defines the generator, it does not generate anything.

It’s the same reason you won’t get output after evaluating

f() = println("AAAA")

How do you generate from a generator? what function does map call to activate the generator?

Thanks

The generator expression returns something iteratable and you just iterate on it. You can manually call the iterator protocol or us anything that iterates an iterator.

Also, if you want to access the result before passing it to map somehow, you should just use a comprehension, which is just a collect on the generator.