Function Repository Resource:

Inline

Source Notebook

Insert the values of symbols into an expression before evaluation

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["Inline"][symbol,expr]

inserts the value of symbol into expr.

ResourceFunction["Inline"][{sym1,sym2,},expr]

inserts the values of the symbols sym1,sym2, into expr.

Examples

Basic Examples (1) 

Inline the value of a symbol into a held expression:

In[1]:=
x := Echo["Hello world"];
In[2]:=
ResourceFunction["Inline"][x, Hold[x]]
Out[2]=

Scope (3) 

Retrieve the ResourceFunction:

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

Retrieve the ResourceObject:

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

Inline multiple symbols:

In[5]:=
x := Echo["Hello world"];
y = 10;
In[6]:=
ResourceFunction["Inline"][{x, y}, Hold[x + y]]
Out[6]=

Generalizations and Extensions (3) 

For values defined with Set, Inline is functionally similar to using With in order to insert values:

In[7]:=
x = 1;
In[8]:=
With[{x = x}, Hold[x]]
Out[8]=
In[9]:=
ResourceFunction["Inline"][x, Hold[x]]
Out[9]=

Inline also works for delayed values without evaluating the value:

In[10]:=
hello := Echo["Hello world"]
In[11]:=
ResourceFunction["Inline"][hello, Hold[hello]]
Out[11]=
In[12]:=
With[{hello := hello}, Hold[hello]]
Out[12]=

Inline a function into an expression:

In[13]:=
ClearAll[f]
f[x_] := x + 1;
f[x_, y_] := x + y;

ResourceFunction["Inline"][f, Hold[Table[{f[n], f[n, n]}, {n, 10}]]]
Out[16]=

Options (1) 

MaxIterations (1) 

Limit the number of replacements that are made:

In[17]:=
f[x_] := f[x + 1];
ResourceFunction["Inline"][f, Hold[f[x]], MaxIterations -> 5]
Out[36]=

Possible Issues (2) 

Patterns are matched using the unevaluated expression:

In[37]:=
ClearAll[f];
f[x_Integer] := x + 1;
f[other_] := $Failed;

ResourceFunction["Inline"][f, Hold[Table[f[n], {n, 10}]]]
Out[40]=

Recursive functions usually cannot be completely eliminated:

In[41]:=
ClearAll[f];
f[0] = 1;
f[x_] := x*f[x - 1];

ResourceFunction["Inline"][f, Hold[f[5]], MaxIterations -> 10]
Out[44]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 15 August 2018

License Information