# Generator Evaluation Order?

**URL:** https://discourse.julialang.org/t/generator-evaluation-order/31823
**Category:** General Usage
**Created:** [December 3, 2019, 9:32pm UTC](https://discourse.julialang.org/t/generator-evaluation-order/31823 "2019-12-03T21:32:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [December 3, 2019, 9:32pm UTC](https://discourse.julialang.org/t/generator-evaluation-order/31823/1 "2019-12-03T21:32:29Z")

</div>

```nohighlight
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

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 3, 2019, 9:43pm UTC](https://discourse.julialang.org/t/generator-evaluation-order/31823/2 "2019-12-03T21:43:48Z")

</div>

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

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

```julia
f() = println("AAAA")

```

---

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [December 3, 2019, 9:48pm UTC](https://discourse.julialang.org/t/generator-evaluation-order/31823/3 "2019-12-03T21:48:11Z")

</div>

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

Thanks

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 3, 2019, 9:51pm UTC](https://discourse.julialang.org/t/generator-evaluation-order/31823/4 "2019-12-03T21:51:21Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 3, 2019, 9:53pm UTC](https://discourse.julialang.org/t/generator-evaluation-order/31823/5 "2019-12-03T21:53:07Z")

</div>

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.
