# Using gensym properly

**URL:** <https://discourse.julialang.org/t/using-gensym-properly/3257>\
**Category:** General Usage\
**Tags:** macros, reactiveprogramming\
**Created:** [April 18, 2017, 12:58am UTC](https://discourse.julialang.org/t/using-gensym-properly/3257 "2017-04-18T00:58:06Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![pyrex41](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pyrex41](https://discourse.julialang.org/u/pyrex41)\
**Post date:** [April 18, 2017, 10:14am UTC](https://discourse.julialang.org/t/using-gensym-properly/3257/5 "2017-04-18T10:14:53Z")

</div>

So, the options you presented all yield the behavior I am looking for; I have simply not expressed myself quite clearly. The macro “@medWin” is only invoked once, but the function inside the macro will be called and need access to “arr” every time an event is fired:

```julia
macro medWin(sig,win = 5)
    arr = []
    quote
        flatmap($sig) do iv
            unshift!($arr, iv)
            length($arr) > $win ? pop!($arr) : false
            median($arr) |> Signal
        end
    end
end

ff = Signal(0)
gg = @medWin(ff,3)

```

Now, we update signal ff and see how this changes our model:

```julia
for i=1:10
    push!(ff, i)
    println("ff is ", ff.value)
    println("gg is ", gg.value)
    println("-----")
end

```

Output:

```julia
ff is 1
gg is 0.5
-----
ff is 2
gg is 1.0
-----
ff is 3
gg is 2.0
-----
ff is 4
gg is 3.0
-----
ff is 5
gg is 4.0
-----
ff is 6
gg is 5.0
-----
ff is 7
gg is 6.0
-----
ff is 8
gg is 7.0
-----
ff is 9
gg is 8.0
-----
ff is 10
gg is 9.0
-----

```

This works as desired, with gg returning the median value of the most recent 3 values of ff. Additionally, arr is not defined in the global scope; as I understand it, it’s in something like a closure. Thank you for your help; I did not realize that I could access the scope within the macro in this manner.

P.S.: obv still using 0.5.1. Where can I learn more about why the need for the escapes have been added to 0.6? Curious on the thought process here.

---

_[View the full topic](https://discourse.julialang.org/t/using-gensym-properly/3257)._
