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 (2) 

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 (2) 

Retrieve the ResourceFunction:

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

Retrieve the ResourceObject:

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

Generalizations & Extensions (2) 

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]]]
Out[7]=
In[8]:=
c2 = ResourceFunction["CompressWithDefinitions"][Unevaluated[f[5]]]
Out[8]=
In[9]:=
ClearAll[f, g];
In[10]:=
Uncompress[c1]
Out[10]=
In[11]:=
Uncompress[c2]
Out[11]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 15 August 2018

Related Resources

License Information