I'm developing a package, and one of its functions is
param, which reads a rather big file and extract one of the parameters in there.
I have a few more functions, say
f2, and so on, whose definitions depend on the value of
param. So straightforwardly I could write
param:=param=(*reading files*) f1:=Switch[param, value1,def11, value2,def12,...] f2:=Switch[param, value1,def21, value2,def22,...] ...
The advantage is that as soon as any one of the
f functions is called,
param will be set, so that when I call other
f functions, I can save some time by avoiding reading and defining
param again. Also,
param will only be defined when it is necessary.
However this code is rather cumbersome, because I have to repeat
Switch many times, and each of the
defs is itself long. A better way could be
Switch[param, value1, f1:=def11;f2:=def21;..., value2, f1:=def12;f2:=def22;..., ..., ];
Now the code is in a more concise style, but the price is that
param will be defined when loading the package, and unnecessarily slowing down the loading when none of the
f functions are to be called.
My question is that, is there a better way to do the job, that takes both of the advantages that (1)
param is only defined when necessary (when the
f functions are called), and (2) the
f functions can be define concisely in a collective way?