d["apple"] = {1, 2, 3};
s["apple"] = 1;
d["banana"] = {10, 20, 30};
s["banana"] = 10;
d["kiwi"] = {100, 200, 300};
s["kiwi"] = 100;
data = {"apple", "banana", "kiwi"};

myfun["model1"][data_, scale_] := Total[data];
myfun["model2"][data_, scale_] := Total[data]/scale;
myfun["model3"][data_, scale_] := Exp[Total[Log[data]]];

The main function that runs:

mymain[data_, scale_, model_] := Block[{answer},
  answer = myfun[model][data, scale]

Say then I make some calls to mymain

mymain[d["apple"], s["apple"], "model1"]
mymain[d["banana"], s["banana"], "model1"]
mymain[d["apple"], s["apple"], "model2"]
mymain[d["banana"], s["banana"], "model3"]
mymain[d["kiwi"], s["kiwi"], "model3"]

How do I edit the code in mymain, so that I have a track of record of what has been done. Say I want to have a look, what have been fitted to model1, I would get the result from running

mymain[d["apple"], s["apple"], "model1"]
mymain[d["banana"], s["banana"], "model1"]

I wonder if possible to have a global variable, say result, that can store all the information. So that when I call result["model1"], it gives me the two quantities I described above.

AND if I want to know, what models have been fitted to the "kiwi" data, I call result["kiwi"], would only get the result from

mymain[d["kiwi"], s["kiwi"], "model3"]

If I then do

mymain[d["kiwi"], s["kiwi"], "model1"]

and result["kiwi"] would give me the results from

mymain[d["kiwi"], s["kiwi"], "model1"]
mymain[d["kiwi"], s["kiwi"], "model3"]

Each call to mymain, the result is stored in result. So I can extract them for later use.

  • $\begingroup$ I do not follow your application of Block. What is it intended to do? $\endgroup$
    – Mr.Wizard
    Jan 22 '15 at 19:05
  • $\begingroup$ @Mr.Wizard That was just a random example. My actual mymain does a lot computations and there will be some local variables. $\endgroup$ Jan 22 '15 at 20:26

I suspect that a somewhat different structure will ultimately benefit you, and I shall attempt to recommend one if and when I better understand your needs. However for the moment these examples might be useful to you:

data = {"apple", "banana", "kiwi"};
models = {"model1", "model2", "model3"};

result[m_List][par_] := result[#][par] & /@ m

result[m_][par_List] := result[m] /@ par




{60, 6, 6000}

{6, 60, 600}

{{6, 60, 600}, {6, 6, 6}, {6, 6000, 6000000}}
  • $\begingroup$ I went through your example, which runs fine. Sorry if I was not clear. But I hope to keep the current structure of the main function mymain. What I am looking for is a way to store the results from running the function mymain. After running some particular calls to mymain, I want to be able to extract the results. $\endgroup$ Jan 22 '15 at 20:41
  • $\begingroup$ I have updated my question, hope that is now more specific. $\endgroup$ Jan 22 '15 at 20:50
  • $\begingroup$ @Chen I just looked at your update. I am not sure I understand. You wish to update result (or is res different?) ever time mymain is used? Is result not used directly? If you could give an example of the actual input you would ideally like to use and the output or effect that you would like it to have it will help me. $\endgroup$
    – Mr.Wizard
    Jan 23 '15 at 0:29
  • $\begingroup$ really sorry for not being able to explain myself clearly. Please see updated post again. Thanks. $\endgroup$ Jan 23 '15 at 1:34

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.