Re: Storing and Loading Definitions / Emulating Associative Arrays
- To: mathgroup at smc.vnet.net
- Subject: [mg93535] Re: Storing and Loading Definitions / Emulating Associative Arrays
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Thu, 13 Nov 2008 21:07:24 -0500 (EST)
- References: <gf3mfg$ekk$1@smc.vnet.net> <gfefjg$n7m$1@smc.vnet.net> <gfgqh1$dkn$1@smc.vnet.net>
Nikolaus Rath wrote:
>
> Yes, DumpSave (and Save) already looks quite promising. The only thing
> that I don't like about it is the fact that the name of the definition
> is saved as well. I would like to e.g. save the definitions of a
> function f and then load them as definitions for a function g (without
> affecting an already defined f). Is there a way to accomplish that?
I am not sure this is the most elegant method to achieve this, but
suppose you have a function f that you want to save away:
In[215]:= f[a_]:=g[a];
f[3]=42;
In[218]:= tempstuff=DownValues[f]/.f->tmpfun
Out[218]= {HoldPattern[tmpfun[3]]:>42,HoldPattern[tmpfun[a_]]:>g[a]}
Now suppose you use DumpSave to save tempstuff (tempstuff and tempfun
can be private variables in a package when you want to make this really
robust).
Restoring tempstuff later, the function definition can be transfered to
a symbol h thus:
In[6]:= DownValues[h]=tempstuff/.tmpfun->h
Out[6]= {HoldPattern[h[3]]:>42,HoldPattern[h[a_]]:>g[a]}
In[7]:= ?h
Global`h
h[3]:=42
h[a_]:=g[a]
David Bailey
http://www.dbaileyconsultancy.co.uk