Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Find subexpressions together with their positions
ResourceFunction["SubexpressionPositions"][expr,opts] returns an association with position and subexpression of expr that match certain criteria provided in opts. | |
ResourceFunction["SubexpressionPositions"][expr,levelspec, opts] finds only subexpressions that appear on the levels specified by levelspec. | |
ResourceFunction["SubexpressionPositions"][expr,levelspec, n, opts] return only first n results. | |
ResourceFunction["SubexpressionPositions"][expr,positions, opts] select from a given list of positions. |
"OuterMatch" | _→_ | outer expressions matching criteria |
"InnerMatch" | _→_ | inner expressions matching criteria |
"OuterMode" | "Any" | "Any" or All |
"InnerMode" | "Any" | "Any" or All |
"Complement" | False | take a complement of an overall criteria |
Heads | True | whether to include expression heads |
Find all subexpressions:
In[1]:= |
Out[1]= |
Specify level:
In[2]:= |
Out[2]= |
Use explicit positions:
In[3]:= |
Out[3]= |
Find subexpressions that match a pattern:
In[4]:= |
Out[4]= |
In[5]:= |
Out[5]= |
Find subexpressions with any inner expression matching a specified pattern:
In[6]:= |
Out[6]= |
Find subexpressions with any outer expression matching a pattern:
In[7]:= |
Out[7]= |
Find subexpressions that have any inner expression matching a specified pattern and relative position:
In[8]:= |
Out[8]= |
Find subexpressions that have any outer expression matching a specified pattern and are positioned at given relative position to it:
In[9]:= |
Out[9]= |
Find subexpressions with any inner subexpression not matching a specified pattern:
In[10]:= |
Out[10]= |
Find subexpressions with all inner subexpressions not matching a specified pattern:
In[11]:= |
Out[11]= |
Find subexpressions with all outer expressions matching a specified pattern:
In[12]:= |
Out[12]= |
Find subexpressions with combined outer and inner matching patterns:
In[13]:= |
Out[13]= |
Find specific heads:
In[14]:= |
Out[14]= |
Use Unevaluated to delay evaluation:
In[15]:= |
Out[15]= |
Specify an explicit list of positions with use of All and Span with UpTo:
In[16]:= |
Out[16]= |
Return only given number of subexpressions:
In[17]:= |
Out[17]= |
Find subexpressions with all outer expressions matching a given pattern. Positions {1} and {1,1} corresponding to g[x] and x are not present due to having an outer expression matching _g:
In[18]:= |
Out[18]= |
Find subexpressions matching outer expression with relative position. Positions {2} and {2,2} corresponding to g[y,z] and z are not present because they both have relative position {2}:
In[19]:= |
Out[19]= |
Find subexpressions having all inner expressions matching a given pattern. Positions {2} and {2, 1} of expressions g[y] and y, respectively, are not present due to both having an inner expression matching y:
In[20]:= |
Out[20]= |
Find subexpressions having all inner expressions matching any but the given pattern and relative position. Position {2} corresponding to g[y,z] is not present because it has the inner expression matching y at relative position {1}:
In[21]:= |
Out[21]= |
Take a complement to the provided criteria:
In[22]:= |
Out[22]= |
Ignore heads:
In[23]:= |
Out[23]= |
Ignore subexpressions of patterns, conditions and pattern tests, except pattern variables themselves:
In[24]:= |
Out[24]= |
Find subexpressions of list elements together with labeled elements:
In[25]:= |
Out[25]= |
This work is licensed under a Creative Commons Attribution 4.0 International License