Hi all,
I am trying to store a vector of functions, which can later be retrieved and applied to one or more vectors. An example:
using ShiftedArrays
fs = Function[ShiftedArrays.lag, diff]
x = rand(10)
for f in fs
x = fs(x)
end
The above works. My issue is that I don’t know how to do it (optimally) if I want to specify some of the argument values before storing them. Let’s say that I want the third lag, e.g. lag(x, n = 3). While I could store this as, say, lag3(x) = ShiftedArrays.lag(x, 3), the argument ‘n’ is randomly selected when the code runs, and hence I would have to pre-define maybe 100 such functions lag1(x)…lag100(x) which seems not ideal.
I have thought about storing this in structs, but don’t know if this is the “correct”/optimal way to go either, e.g.:
using ShiftedArrays
mutable struct Lag
n::Int
end
my_lag(x::AbstractVector, t::Lag) = ShiftedArrays.lag(x, t.n)
x = rand(10)
my_lag(x, Lag(3)
Going down this road, I would store the struct Lag(3) for later usage.
Is there a more elegant way to solve this given that the lag parameter ‘n’ isn’t determined until the program runs and with the understanding that there may be many more functions than just lag and diff?
Thank you in advance!