I want to define a function fun, a function funResult such and a function HowManyTimesHasBeenRunning such that

In[1]:= fun[]
Out[1]= result1

In[2]:= fun[]
Out[2]= result2

In[3]:= fun[]
Out[3]= result3

In[4]:= funResult
Out[4]= {result1,result2,result3}

In[5]:= HowManyTimesHasBeenRunning 
Out[5]= 3
  • $\begingroup$ I think this should answer your question: how to generate repeatable Unique streams? $\endgroup$ – Kuba Mar 2 '17 at 9:15
  • $\begingroup$ @Kuba Good link.I will edit my question to show the difference with that question. :) $\endgroup$ – yode Mar 2 '17 at 9:18

Based on linked topic

Module[{i = 0}
  , funResult := fun /@ Range@HowManyTimesHasBeenRunning
  ; HowManyTimesHasBeenRunning := i
  ; fun[] := fun[i] = (++i)^4

I'm not appeding to funResult but memoize those results in fun to make it faster when fun is called repeatedly.

Table[fun[], 5]
{1, 16, 81, 256, 625}

{1, 16, 81, 256, 625, 1296}
| improve this answer | |
  • $\begingroup$ Still confused,because your fun is based on HowManyTimesHasBeenRunning totally.But if my fun is fun[n_]:=N[Tanh[n]].How to make it have that memory function? $\endgroup$ – yode Mar 2 '17 at 10:08
  • $\begingroup$ There is no fun[asd_] request in your question. Make sure to include all needed features. $\endgroup$ – Kuba Mar 2 '17 at 10:13
  • $\begingroup$ I should add the feature I need or post another question?Sometimes,the implement-method will change the topic a little.I realized that question after I read your answer.I'm feel sorry for that.. $\endgroup$ – yode Mar 2 '17 at 10:21
  • $\begingroup$ @yode At this point it is probably better to ask another one. But make sure you have thought everything through before you post. $\endgroup$ – Kuba Mar 2 '17 at 10:28

I would assign UpValues to the symbol fun. See the documentation for TagSet and UpSet, which are interchangeable here:

(* Initialize, UpSet syntax *) 
In[1]:= HowManyTimesHasBeenRunning[fun] ^= 0;
funResult[fun] ^= {};

(* Function definition, TagSet syntax *)
In[3]:= fun[] := (
  fun /: HowManyTimesHasBeenRunning[fun] = 
   HowManyTimesHasBeenRunning[fun] + 1;
  fun /: funResult[fun] = Append[funResult[fun], result1]

In[4]:= fun[]
Out[4]= {result1}

In[5]:= fun[]
Out[5]= {result1, result1}

In[6]:= fun[]
Out[6]= {result1, result1, result1}

In[9]:= HowManyTimesHasBeenRunning[fun]
Out[9]= 3

In[10]:= funResult[fun]
Out[10]= {result1, result1, result1}

By using UpValues, all values are assigned to the symbol fun instead of HowManyTimesHasBeenRunning and funResult, which remain undefined:

In[11]:= Definition[fun]
fun[]:=(fun/:HowManyTimesHasBeenRunning[fun] = HowManyTimesHasBeenRunning[fun]+1;

In[16]:= Defintion[HowManyTimesHasBeenRunning]
Out[16]= Defintion[HowManyTimesHasBeenRunning]

Note that I just wrote result1 every time. You may of course replace that with some actual code.

| improve this answer | |
  • $\begingroup$ Inspiring!Having you see my this answer? $\endgroup$ – yode Mar 2 '17 at 14:00
  • $\begingroup$ Same idea obviously. But why did you repost the question and not refine this one? $\endgroup$ – Theo Tiger Mar 2 '17 at 14:10
  • $\begingroup$ Kuba suggest that it will be mess.Anyway,thanks all the same. :) $\endgroup$ – yode Mar 2 '17 at 14:17

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.