3
$\begingroup$

I want to save expressions as well as their names in a file.

  func[i_] := i;
  Do[func[i] >>> out.m,{i,1,3}];

The output is

   cat out.m

   1
   2
   3

However the desired output is

   cat out.m

   func[1] = 1;
   func[2] = 2;
   func[3] = 3;

Save does not save here.

$\endgroup$
6
  • $\begingroup$ What about DumpSave? $\endgroup$ – CA Trevillian Oct 13 '19 at 21:51
  • $\begingroup$ I do not know, but with that also I am unable to produce the expected output. $\endgroup$ – Boogeyman Oct 14 '19 at 9:21
  • 1
    $\begingroup$ What do you want to export, the function names alone, or the definitions? The syntax you have there would result in the out.m being overwritten each time. I think you’re looking for something like OpenWrite see here for some other examples. $\endgroup$ – CA Trevillian Oct 14 '19 at 9:31
  • $\begingroup$ With DumpSave you should be able to use the third option down in the templated examples, for this question. $\endgroup$ – CA Trevillian Oct 14 '19 at 9:34
  • $\begingroup$ The overwriting is a different thing. My issue here is to write the function name as well as its definition/expression. Save would have worked if I just define just say fun1=func[1], then it would have written inside out.m as fun1=1. But I want func[1]=1 in the output. And in a generic case this func definition may not be this simple. $\endgroup$ – Boogeyman Oct 14 '19 at 9:37
5
$\begingroup$

You can use Save in this way:

func[i_] := func[i] = i;
Do[func[i], {i, 1, 3}];
FullDefinition@func
(*
func[1] = 1

func[2] = 2

func[3] = 3

func[i_] := func[i] = i
*)

Save["out.m",func];
ClearAll[func]; Get["out.m"]; FullDefinition@func
(*
func[1] = 1

func[2] = 2

func[3] = 3

func[i_] := func[i] = i
*)

So, here, we show that memoization can be used as a method to record the defined expressions when one is going to save them externally.

Your suggested method of using Save was not exactly proper, which is why it would not work for you. Here, this plays out as expected. While the memoized func name cannot be avoided in this procedure, it seems to do as you would like.

Does this make sense?

$\endgroup$
2
$\begingroup$
func[i_] := i

Do[ 
    "func["<>ToString@i<>"] = "<>ToString@func@i >>> "out.m"
   , {i,1,3}
  ]

Edit 1: It's not clear to me what's your scope but, this does what you asked

"func["<>ToString@#<>"] = "<>ToString@func@# & /@ Range@3 // 
Export["out.m", #~StringRiffle~"\n"<>"\n","Text"]& 

Edit 2: Given that your scope is to create a file that contains only the DownValues of a symbol f, you do not need to define a substitution rule (:=) for f. So, let's just define the DownValues of f and export them in a text file

Remove@f    

f@#~Set~# &/@ Range@3

ToString@Information[f]@"DownValues" // 
Export["out.m",#,"Text"]&
$\endgroup$
6
  • $\begingroup$ It produces "func[1]=1". Without running shell commands is it possible to remove " ? $\endgroup$ – Boogeyman Oct 13 '19 at 9:37
  • $\begingroup$ @Boogeyman see Edit 1 $\endgroup$ – Fortsaint Oct 13 '19 at 9:45
  • $\begingroup$ The scope is very clear if you want to create an output which you can directly translate from Mathematica to other languages eg. symbolic manipulation system like Form. :) $\endgroup$ – Boogeyman Oct 13 '19 at 9:49
  • $\begingroup$ Why you define funcin advance? you could have just defined another function g[i_]:=1 and then append f[i] = g[i]. If you see @Fraccalo answer, it has all those Hold-related function solely because you had defined func in advance $\endgroup$ – Fortsaint Oct 13 '19 at 9:56
  • $\begingroup$ Yes I can do that. But this is the whole point. You have to define just another function in order to save it in a nice form. My question is very simple. You had a function definition and you want to print it with its expression as well as its names. Now if you do define , its just extra definition which to me is not justified just for printing. This is why I wanted to ask if there is nicer way to handle. $\endgroup$ – Boogeyman Oct 13 '19 at 10:04
1
$\begingroup$
list = ToString[#] <> "=" <> ToString[ReleaseHold@#] <> ";" &@
    HoldForm@func[#] & /@ Range[3]

(# >>> out.m) & /@ list
$\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.