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:
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:
for i=1:10
push!(ff, i)
println("ff is ", ff.value)
println("gg is ", gg.value)
println("-----")
end
Output:
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.