5
$\begingroup$

I am sure this has been asked before in a way or another. I need to Save or DumpSave some symbols that contain the result of some processing.

Currently my code has a low level function that does the dirty work, and a high level one that is a handy proxy to call the low level one.

Here is how my code schematically works:

   LowLevelFunction[ba_] := Module[{res},
    Table[res[elem] = 
    If[ToString[ba] == "yes", X, 2 X], {elem, {one, two}}];
   res]

  HighLevelFunction[a_] := Module[{res}, res = LowLevelFunction[a]; res]

Then I use these functions as:

  Processed[yes] = HighLevelFunction[yes]
  Processed[maybe] = HighLevelFunction[maybe]

At this point ?Processed returns Processed[maybe]=res$2748 and Processed[yes]=res$2745. I am happy with this so far because I can evaluate in the current notebook and get the answer that I want

   Processed[yes][one]
   Processed[maybe][two]

   Out[270]= X

   Out[271]= 2 X

Furthermore when I do Save["~/fileprocessed.mx", Processed] I get a mx file that contains a reference to res$2748 and res$2745 and the actual symbols res$2748 and res$2745. Therefore I can do Get["~/fileprocessed.mx"] on a new notebook and a new kernel and

    Processed[yes][one]
    Processed[maybe][two]

will give me the right output.

Now my question is if there is a better way of handling this. For instance a way that avoid having to use Temporary objects, that, for instance, would not be saved if I use DumpSave instead of Save.

$\endgroup$
6
$\begingroup$

Generate and export Unique variables from LowLevelFunction.

LowLevelFunction[ba_] := Module[{res}, res = Unique["res"]; 
  Table[res[elem] = If[ToString[ba] == "yes", X, 2 X], {elem, {one, two}}]; res]

These variables are not temporary, and you should be able to save them

$\endgroup$
3
$\begingroup$

First thing to note is that your HighLevelFunction is not relevant here. When it's evaluated, res variable, inside Module, evaluates to result of LowLevelFunction call.

res$... symbols you're seeing are coming from Module in your LowLevelFunction. In this module you're saving your calculation results as DownValues of temporary module variable res.

If you use Mathematica v10, then instead of auxiliary symbol with DownValues, you can use an Association:

ClearAll[LowLevelFunction, HighLevelFunction]

LowLevelFunction[ba_] := 
    Association[Table[elem -> If[ToString[ba] == "yes", X, 2 X], {elem, {one, two}}]]

HighLevelFunction[a_] := Module[{res}, res = LowLevelFunction[a]; res]

By calling HighLevelFunction with redefined LowLevelFunction we get:

Processed[yes] = HighLevelFunction[yes]
(* <|one -> X, two -> X|> *)
Processed[maybe] = HighLevelFunction[maybe]
(* <|one -> 2 X, two -> 2 X|> *)

You can access values, associated with certain keys, like in your original implementation:

Processed[yes][one]
(* X *)
Processed[maybe][two]
(* 2X *)

You can use Save and DumpSave and everything will work as expected.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.