Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Map a function at parts of an expression that match a given pattern
ResourceFunction["MapCases"][f,{e1,e2,…},pattern] applies f to each ei that matches pattern. | |
ResourceFunction["MapCases"][f,expr,pattern,levelspec] applies at levels specified by levelspec. | |
ResourceFunction["MapCases"][f,pattern] represents an operator form of ResourceFunction["MapCases"] that can be applied to an expression. |
Map a function at integers only:
| In[1]:= |
| Out[1]= |
Map a function at anything other than integers:
| In[2]:= |
| Out[2]= |
Use the operator form of MapCases:
| In[3]:= |
| Out[3]= |
| In[4]:= |
| Out[4]= |
Map at matching elements in an Association:
| In[5]:= |
| Out[5]= |
Apply a function to strings only:
| In[6]:= |
| Out[6]= |
Apply a function to cases of lists of integers:
| In[7]:= |
| Out[7]= |
Apply a function to nonzero values only:
| In[8]:= |
| Out[8]= |
Apply a function to cases of lists of integers at any level:
| In[9]:= |
| Out[9]= |
Map at symbols among the elements of an expression:
| In[10]:= |
| Out[10]= |
Also include the head of the expression in the search:
| In[11]:= |
| Out[11]= |
By default, the f[ei] will remain unevaluated if the ei is found within a held expression:
| In[12]:= |
| Out[12]= |
Evaluate each of the f[ei] when applying:
| In[13]:= |
| Out[13]= |
Convert all symbols in an expression to strings:
| In[14]:= |
| Out[14]= |
Include heads:
| In[15]:= |
| Out[15]= |
MapCases[f,expr,_] is equivalent to Map[f,expr]:
| In[16]:= |
| Out[16]= |
| In[17]:= |
| Out[17]= |
| In[18]:= |
| Out[18]= |
MapCases[f,expr,pattern,levelspec] is equivalent to Replace[expr,e:pattern⧴f[e],levelspec]:
| In[19]:= |
| Out[19]= |
| In[20]:= |
| Out[20]= |
| In[21]:= |
| Out[21]= |
MapCases[f,expr,pattern,levelspec] is equivalent to MapAt[f,expr,Position[expr,pattern,levelspec]]:
| In[22]:= |
| Out[22]= |
| In[23]:= |
| Out[23]= |
| In[24]:= |
| Out[24]= |
When using the option "EvaluateCases"→True, these equivalencies no longer necessarily hold:
| In[25]:= |
| Out[25]= |
| In[26]:= |
| Out[26]= |
| In[27]:= |
| Out[27]= |
| In[28]:= |
| Out[28]= |
| In[29]:= |
| Out[29]= |
MapCases by default starts at level 1, so it does not apply the function to the whole expression:
| In[30]:= |
| Out[30]= |
| In[31]:= |
| Out[31]= |
In MapCases[f,expr,pattern], the pattern will evaluate before matching unless wrapped in HoldPattern:
| In[32]:= |
| Out[32]= |
| In[33]:= |
| Out[33]= |
Highlight integers in an expression:
| In[34]:= |
| Out[34]= |
| In[35]:= |
| Out[35]= |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License