Wolfram Research

Function Repository Resource:

CompressWithDefinitions

Source Notebook

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

Contributed by: Richard Hennigan (Wolfram Research)

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]=

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]:=
Uncompress[compressed, HoldForm]
Out[5]=

Compress by itself does not preserve definitions:

Out[5]=
Out[5]=
In[6]:=
Uncompress[c1]
Out[6]=
In[7]:=
Uncompress[c2]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information