I'm working on a package with a lot of functions using Module, which all begin by unpacking the arguments into local variables. I'd like to store this unpacking code in a separate function called Setup and just call it in each function, but the local variables don't make it out.
A simple example:
Setup := (z = x^2);
g[x_] := Module[{z},
Setup;
Return[z]
];
g[3]
returns z$2599
when I'd like it to return 9
. My real Setup is much longer of course, which is why I'd rather recycle it than rewrite it in each function.
Is there an easy way to make this work?
Update: A less minimal example
Here's a slightly less minimal example that captures more of the real situation. In my real problem, the Setup
code is about a page long. At this point, I'm too far into this to change how I store the z[i]
to a list!
Setup := (
numX = Length[x];
Do[z[i] = StringLength[x[[i]]], {i, numX}]
)
g[x_List] := Module[{numX, z},
Setup;
Table[z[i], {i, numX}]
]
g[{"hi", "there"}]
should return {2,5}
.
setup[param_(*if required*),x_]:=x^2; g[x_]:=Module[{ret,z}, z=setup[param,x]; ret=f[z](*if required*); Return[ret] ];
g[3] $\endgroup$Return
at the end of aModule
? Even though unnecessary, it helps me easily spot what's being returned at the end of a longModule
. $\endgroup$