I don’t think it’s a good idea to rely on a global variable that you will ‘mostly’ use. Using eval
, too, in a context like this seems ill-advised.
What about something like this:
jl> pdf_X(X::Distribution{Univariate, Discrete}) = OffsetArray(pdf(X), -1);
jl> f_X = pdf_X(DiscreteUniform());
jl> f_X[0]
0.5
jl> f_X[0:1]
2-element Vector{Float64}:
0.5
0.5
jl> f_X[:]
2-element OffsetArray(::Vector{Float64}, 0:1) with eltype Float64 with indices 0:1:
0.5
0.5
?
# My method:
jl> @btime $f_X[$0]
1.100 ns (0 allocations: 0 bytes)
0.5
# your method
jl> @btime $f_X($0)
194.167 ns (3 allocations: 144 bytes)
0.5