I need to define a whole group of functions, each of which depends on large heap allocated arrays which do not change run to run. While I could declare these arrays const, this clutters the namespace as the array names used are similar for each function, and defining a separate module to separate the namespace for each is something I would rather not do. Here is a toy example of such a definition:
const B = [1,2,45,3]
non_alloc_const(x) = sum(x^a for a in B)
However, declaring the arrays within the function leads to them being reallocated each time, as in the function below, which is undesirable given the size of the arrays.
function alloc(x)
A = [1,2,45,3]
return sum(x^a for a in A)
end
And something like the below code does exactly what I want, but I tend to develop by repeatedly executing the code in the same REPL in vscode, so an error is thrown as the const is redefined every time. Removing the const makes the function a non-constant global and rather slow.
const non_alloc_let = let
A = [1,2,45,3]
g(x) = sum(x^a for a in A)
end
Is there another, easier way to define such a function? Am I missing something obvious?