Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Group elements of a list using patterns
ResourceFunction["GroupCases"][{e1,e2,…},patt] groups the ei by whether or not they match patt and returns the Association <|patt→{…}, _→{…}|>. | |
ResourceFunction["GroupCases"][{e1,e2,…},patt⧴rhs] applies a transformation to the elements that match patt. | |
ResourceFunction["GroupCases"][list, {spec1,spec2,…}] returns <|spec1→{…},spec2→{…},_→{…}|>. | |
ResourceFunction["GroupCases"][list,<|key1→spec1,key2→spec2,…|>] returns <|key1→{…},key2→{…},_→{…}|>. | |
ResourceFunction["GroupCases"][spec] represents an operator form of ResourceFunction["GroupCases"]. |
Find all integers and non-integers in a List:
| In[1]:= |
| Out[1]= |
Apply a transformation to the matching elements:
| In[2]:= |
| Out[2]= |
Use multiple patterns to split the values by type:
| In[3]:= |
| Out[3]= |
Transformations can mixed with normal patterns:
| In[4]:= |
| Out[4]= |
Define custom keys for labeling the returned values:
| In[5]:= | ![]() |
| Out[5]= |
Replacement rules can also be labeled:
| In[6]:= | ![]() |
| Out[6]= |
GroupCases can be defined as an operator:
| In[7]:= | ![]() |
| Out[8]= |
GroupCases always matches each element exactly once and all elements are returned under one of the keys in the output:
| In[9]:= |
| Out[10]= |
If no transformations are used, the following relation will hold:
| In[11]:= |
| Out[11]= |
All specified patterns will be included as a key in the output Association, even if no value is found for that pattern:
| In[12]:= |
| Out[12]= |
This work is licensed under a Creative Commons Attribution 4.0 International License