MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: something nice I found today, return multiple values from a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113134] Re: something nice I found today, return multiple values from a function
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Wed, 13 Oct 2010 23:29:20 -0400 (EDT)
  • References: <i927an$3io$1@smc.vnet.net> <i93kbo$e67$1@smc.vnet.net>
  • Reply-to: nma at 12000.org

On 10/12/2010 11:42 PM, Albert Retey wrote:

>>
>
> 1) I think it has been mentioned in another post today: Module with an
> empty list is just an elaborate noop: You better just say:
>
>   getPointCoordinates[x_, y_] := {x ->  10, y ->  7}
>

Ok, but I was just using an example to illustrate the main point of how 
to package multiple return values neatly.


> 2) you also might like this:
>
>
> makepoint[p_Symbol,xx_,yy_]:=(p[x]=xx;p[y]=yy; p);
>
> makepoint[p,10,7]
>
> p@x
> p@y
>
> hth,
>
> albert
>

Well, after more playing around, I know settled on this 'pattern'.

But before I show it, let me be clear what I am trying to do: Sometimes 
I call a Module[] with some input parameters, and it does some 
computation, and return back number of results. I used to return the 
result in a LIST to the caller.  Had to make sure I access the return 
values from the list on return in the same order they are put in.

A struct would have solved this. But now I can do this, please let me 
know what you think of this new way:

In[11]:= computeSomething[r_] := Module[{var1 = -99, var2 = 20, result}, 
result["x"] = var1*r; result["y"] = var2*r;
      result];
In[12]:= result = computeSomething[30];
x = result["x"]
y = result["y"]
Out[13]= -2970
Out[14]= 600


---- define some module to do some work and return many results

computeSomething[r_] := Module[{var1 = -99, var2 = 20, result},
       result["x"] = var1*r;
       result["y"] = var2*r;
       result]


---- now call it with some data to compute something
result = computeSomething[30];

--- now result is in a struct-like, I can access the
--- result by NAME, which is important

x = result["x"]
y = result["y"]
Out[9]= -2970
Out[10]= 600

---------------------------

Now I think I have what seems like a good struct emulation in 
Mathematica. unless I found a problem with this 'pattern', I think I 
will start using it.

The above is better than what I used to do which is:

------------------------------
computeSomething[r_] := Module[{var1 = -99, var2 = 20, result},
     result = {var1*r, var2*r}
];

result = computeSomething[30];

x = result[[1]]
y = result[[2]]
Out[7]= -2970
Out[8]= 600
-------------------------------------

The difference is that now I do not have to worry about which position 
each individual result is at.  Instead of result[[1]], I write result["x"]

Thanks all for the input.

--Nasser


  • Prev by Date: How to explain these variations in execution speeds?
  • Next by Date: Re: What is the ESC sequence for the "Matching Double Brakets"? From
  • Previous by thread: Re: something nice I found today, return multiple values from a function
  • Next by thread: Re: something nice I found today, return multiple values from a function