Function Repository Resource:

# ExpressionToFunction

Convert an expression to a pure function by specifying which symbols should be used as input arguments

Contributed by: Sjoerd Smit
 ResourceFunction["ExpressionToFunction"][expr,var1,var2,…] returns Function[{var1,var2,…},expr]. ResourceFunction["ExpressionToFunction"][expr,…,{vari,1,vari,2,…},…] bundles {vari,1,vari,2,…} together in one function slot as a vector argument. ResourceFunction["ExpressionToFunction"][expr,varspec1→index1,varspec2→index2,…] binds variables specified by varspeci to Slot[indexi].

## Details and Options

ResourceFunction["ExpressionToFunction"] can be used as an alternative to ReplaceAll (/.) or Block for evaluating symbolic formulas at given variable values.
ResourceFunction["ExpressionToFunction"] holds all of its arguments and should work even when the variables already have definitions.
You can use ResourceFunction["ExpressionToFunction"][Evaluate[expr],] if you want to pass expr in evaluated form.
Indexed is used to vectorize variables together.
The attributes of the returned function can be specified with the Attributes option.
In ExpressionToFunction[expr,varspec1 index1,varspec2 index2,] the indexi can be either integers or strings. String indices can be used together with associations. If strings are used as indices, it is up to the user to make sure the strings do not contain illegal characters (see Slot).
When using indexed slots, the returned function is of the form Function[Null,expr,{attribute1, }]. This form makes it possible to use Function attributes in a Function that uses Slot for slotting the arguments.

## Examples

### Basic Examples

Create a function from a simple polynomial:

 In[1]:=
 Out[1]=

Evaluate the polynomial at a given value:

 In[2]:=
 Out[2]=

Convert a multivariate PDF to a function:

 In[3]:=
 Out[3]=
 In[4]:=
 Out[4]=
 In[5]:=
 Out[5]=

Bind arguments to keys in an Association:

 In[6]:=
 Out[6]=
 In[7]:=
 Out[7]=

Bind multiple symbols to a single slot:

 In[8]:=
 Out[8]=
 In[9]:=
 Out[9]=
 In[10]:=
 Out[10]=

### Options

Use the Attributes option to return a function that holds its arguments:

 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=
 In[13]:=
 Out[13]=
 In[14]:=
 Out[14]=

### Applications

Group x and y together as a vector argument and map over a list of points:

 In[15]:=
 Out[15]=
 In[16]:=
 Out[16]=
 In[17]:=
 In[18]:=
 In[19]:=
 Out[19]=

Add a parameter of the PDF as an argument:

 In[20]:=
 Out[20]=
 In[21]:=
 Out[21]=

Convert the solution of a differential equation to a function:

 In[22]:=
 Out[22]=
 In[23]:=
 Out[23]=
 In[24]:=
 Out[24]=

Represent the function at parameter value a=10 with Curry, then map over a range of x values:

 In[25]:=
 Out[25]=

## Requirements

Wolfram Language 11.3 (March 2018) or above