Wolfram Research

Function Repository Resource:

CopyDefinitions

Source Notebook

Copy the definitions of one symbol to another

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["CopyDefinitions"][f,g]

copies the definitions of f into the definitions of g.

Details and Options

The copied symbol will not be dependent on the original symbol as it would be with Set or SetDelayed.

Examples

Basic Examples

Use the function:

In[1]:=
ClearAll[f, g];
f[x_] := x + 1;
ResourceFunction["CopyDefinitions"][f, g];
Definition[g]
Out[1]=

Scope

Retrieve the ResourceFunction:

In[2]:=
ResourceFunction["CopyDefinitions"]
Out[2]=

Retrieve the ResourceObject:

In[3]:=
ResourceObject["CopyDefinitions"]
Out[3]=

Generalizations & Extensions

Using Set or SetDelayed can achieve similar behavior:

In[4]:=
ClearAll[f, g];
f[0] = 1;
f[x_Integer?Positive] := f[x - 1]*x;
g = f;
Table[{f[i], g[i]}, {i, 5}]
Out[4]=

However, the new symbol will still be dependent on the old:

In[5]:=
Definition[g]
Out[5]=
In[6]:=
ClearAll[f];
g[5]
Out[6]=

CopyDefinitions will eliminate dependencies on the original:

In[7]:=
ClearAll[f, g];
f[0] = 1;
f[x_Integer?Positive] := f[x - 1]*x;
ResourceFunction["CopyDefinitions"][f, g];
Table[{f[i], g[i]}, {i, 5}]
Out[7]=
In[8]:=
Definition[g]
Out[8]=
In[9]:=
ClearAll[f];
g[5]
Out[9]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information