How to | Work with Pure Functions
The ability to define and use your own functions is part of what gives Mathematica such power. It is often inconvenient to have to explicitly name a function for every small operation that you wish to perform. Mathematica lets you declare functions inline (called pure functions) to get around this.
The most transparent way to define a pure function is with Function. The first argument is a list of arguments, and the second is a function. This function adds its two arguments together:
| In[1]:= |
| Out[1]= |
| In[2]:= |
| Out[2]= |
You do not need to give the function a name to use it:
| In[3]:= |
| Out[3]= |
A common shorthand notation uses an
to mark the end of the pure function, with argument positions specified by
,
, and so on:
| In[4]:= |
| Out[4]= |
| In[5]:= |
| Out[5]= |
The advantage of a pure function is that it does not require a separate definition or a name:
| In[6]:= |
| Out[6]= |
If the pure function only has one argument, you can use
instead of
. This function squares its argument:
| In[7]:= |
| Out[7]= |
Pure functions become quite powerful when used together with Map.
This makes a list of complex numbers into a list of ordered pairs:
| In[8]:= |
| Out[8]= |
This turns the ordered pairs back into complex numbers:
| In[9]:= |
| Out[9]= |
You can use
as a shorthand for Map. Use a pure function to create a list of clickable buttons:
| In[2]:= |
| Out[2]= |
