Function Repository Resource:

# MapCases

Map a function at parts of an expression that match a given pattern

Contributed by: Richard Hennigan (Wolfram Research)
 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.

## Details and Options

The expr can have any head, not just List.
ResourceFunction["MapCases"][f,pattern][expr] is equivalent to ResourceFunction["MapCases"][f,expr,pattern].
When used on an Association, ResourceFunction["MapCases"] matches elements according to their values.
ResourceFunction["MapCases"] uses standard level specifications:
 n levels 1 through n Infinity levels 1 through Infinity {n} level n only {n1,n2} levels n1 through n2
The default value for levelspec in ResourceFunction["MapCases"] is {1}.
A positive level n consists of all parts of expr specified by n indices.
A negative level -n consists of all parts of expr with depth n.
Level -1 consists of numbers, symbols and other objects that do not have subparts.
Level 0 corresponds to the whole expression.
With the option setting , ResourceFunction["MapCases"] looks at heads of expressions and their parts.
ResourceFunction["MapCases"] traverses the parts of expr in a depth-first order, with leaves visited before roots.
ResourceFunction["MapCases"] has the following options:
 Heads False whether to check heads of subexpressions EvaluateCases False whether to evaluate f[ei] inside held expressions

## Examples

### Basic Examples (3)

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]=

### Scope (4)

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]=

### Options (4)

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]=

#### EvaluateCases (2)

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]=

### Applications (2)

Convert all symbols in an expression to strings:

 In[14]:=
 Out[14]=

 In[15]:=
 Out[15]=

### Properties and Relations (4)

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:patternf[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]=

### Possible Issues (2)

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]=

### Neat Examples (1)

Highlight integers in an expression:

 In[34]:=
 Out[34]=
 In[35]:=
 Out[35]=

## Requirements

Wolfram Language 11.3 (March 2018) or above

## Version History

• 1.0.0 – 18 February 2019