Wolfram Computation Meets Knowledge

CompressWithDefinitions

Contributed by: Richard Hennigan (Wolfram Research)

Compress an expression along with any needed definitions so that it can be used in another session

ResourceFunction["CompressWithDefinitions"][expr]

compresses expr along with its dependent definitions

Examples

Basic Examples

Compress an expression with dependent definitions:

In[1]:=
ClearAll[f, g];
f[x_] := g[x] + 1;
g[x_] := 2 x;
compressed = ResourceFunction["CompressWithDefinitions"][f]
Out[1]=

Definitions are restored when uncompressed:

In[2]:=
ClearAll[f, g];
Uncompress[compressed][5]
Out[2]=

More Examples

Scope

Retrieve the ResourceFunction:

In[3]:=
ResourceFunction["CompressWithDefinitions"]
Out[3]=

Retrieve the ResourceObject:

In[4]:=
ResourceObject["CompressWithDefinitions"]
Out[4]=

Generalizations & Extensions

The compressed expression is an evaluation that restores definitions before returning the output:

In[5]:=
ClearAll[f, g];
f[x_] := g[x] + 1;
g[x_] := 2 x;
compressed = ResourceFunction["CompressWithDefinitions"][f];
In[6]:=
Uncompress[compressed, HoldForm]
Out[6]=

Compress by itself does not preserve definitions:

In[7]:=
c1 = Compress[Unevaluated[f[5]]]
c2 = ResourceFunction["CompressWithDefinitions"][Unevaluated[f[5]]]
Out[7]=
Out[7]=
In[8]:=
ClearAll[f, g];
In[9]:=
Uncompress[c1]
Out[9]=
In[10]:=
Uncompress[c2]
Out[10]=

Resource History