1
$\begingroup$

When running a task which needs a long time for computation but returns a simple result, I often do this:

temp = (*some computation*);
Import["re.wl"]; (*there is some definition in "re.wl"*)
re[1] = temp;
Export["re.wl", Definition[re]];

"re.wl" may be:

re[1] = 1;
re[2] = 2;

In this way I can update re[1] in "re.wl" without recalculating re[2].

What I want is to wrap this code, so I wrote this:

(*syms is symbols that need to be stored*)
Module[{cachepath = FileNameJoin[ParentDirectory[path], "DefinitionCache.wl"]},
        cachepath = Export[cachepath, Definition[syms], OverwriteTarget->"KeepBoth"];
        Import[path];
        Import[cachepath];(*new definition in cachepath will overwrite old definition*)
        Export[path, Definition[syms]];
        DeleteFile[cachepath];
    ];

I create a cache file because I don't know how to "save" the definition in memory so I save it in storage. I'm not satisfied with this so I'm wondering how to make a copy of a symbol's definition.

Is there a better to refresh the definition in the file without redundant calculation?

Thanks creidhne for helping me check for grammatical errors!

$\endgroup$
1
  • $\begingroup$ Are you looking for Save? $\endgroup$
    – Jason B.
    Commented Jul 10 at 14:14

1 Answer 1

0
$\begingroup$

You could use "DownValues" to store your values. E.g.:

Clear["Global`*"]
re[1] = a;
dv = DownValues[re]

{HoldPattern[re[1]] :> a}

re[2] = b;
dv = DownValues[re]

{HoldPattern[re[1]] :> a, HoldPattern[re[2]] :> b}

If you later want the value of e.g. "re[2]" and re[2] is not available because it is overwritten or cleared, you may write:

re[2]=.
re[2] /. dv

b
$\endgroup$
6
  • $\begingroup$ Daniel, please explain, if I understand it right. I guess that this definition is only saved within the present session, is it? Or you mean that it can be retrieved in the course of the next session? $\endgroup$ Commented Sep 13, 2023 at 9:00
  • $\begingroup$ This is only saved in the present session. If you want to store it, you may use e.g.: "DumpSave" and "Get" $\endgroup$ Commented Sep 13, 2023 at 9:20
  • $\begingroup$ Thanks, but compared to DownValues, I think Definition is more helpful. Definition contains more information, and when a file generated by Export[_, Definition[_]] is imported, the definitions will be automatically loaded. What really confuses me is that Definition returns nothing but just print something therefore I cannot use a temporary variable(like dv in your answer) to store the old definitions in the memory, so I need to create the cache file. Of course DownValues is enough to store re in my question, but I still want to ask if there is a way I can use Definition. $\endgroup$
    – Crack-Hu
    Commented Sep 14, 2023 at 10:42
  • $\begingroup$ I think your out of luck. From the help: "Definition is an output form; it does not evaluate" $\endgroup$ Commented Sep 14, 2023 at 20:06
  • 1
    $\begingroup$ Yes, I think you are right. $\endgroup$ Commented Sep 15, 2023 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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