Function Repository Resource:

GroupCases

Source Notebook

Group elements of a list using patterns

Contributed by: Sjoerd Smit

ResourceFunction["GroupCases"][{e1,e2,},patt]

groups the ei by whether or not they match patt and returns the Association <|patt{}, _{}|>.

ResourceFunction["GroupCases"][{e1,e2,},pattrhs]

applies a transformation to the elements that match patt.

ResourceFunction["GroupCases"][list, {spec1,spec2,}]

returns <|spec1{},spec2{},_{}|>.

ResourceFunction["GroupCases"][list,<|key1spec1,key2spec2,|>]

returns <|key1{},key2{},_{}|>.

ResourceFunction["GroupCases"][spec]

represents an operator form of ResourceFunction["GroupCases"].

Details and Options

Every element of the List will be returned exactly once, grouped according to the first pattern that it matches.
Unless explicit keys are given in the input as in ResourceFunction["GroupCases"][list,<|key1spec1,key2spec2,|>], every pattern will be included as a key in the returned Association. If the default pattern _ is not already included in the list of patterns, it will be appended at the end.

Examples

Basic Examples (4) 

Find all integers and non-integers in a List:

In[1]:=
ResourceFunction[
 "GroupCases"][{1, 2, 3/2, 3., 1 + I, Pi, x}, _Integer]
Out[1]=

Apply a transformation to the matching elements:

In[2]:=
ResourceFunction["GroupCases"][{1, 2, 3/2, 3., 1 + I, Pi, x}, i_Integer :> i + Pi]
Out[2]=

Use multiple patterns to split the values by type:

In[3]:=
ResourceFunction[
 "GroupCases"][{1, 2, 3/2, 3., 1 + I, Pi, x}, {_Integer, _Rational, _Real, _Complex , _?NumericQ}]
Out[3]=

Transformations can mixed with normal patterns:

In[4]:=
ResourceFunction[
 "GroupCases"][{1, 2, 3/2, 3., 1 + I, Pi, x}, {i_Integer :> i + Pi, _Rational, _Real, _Complex , _?NumericQ}]
Out[4]=

Scope (2) 

Define custom keys for labeling the returned values:

In[5]:=
ResourceFunction["GroupCases"][{1, 2, 3/2, 3., 1 + I, Pi, x},
 <|"Int" -> _Integer, "Frac" -> _Rational, "Float" -> _Real, "Complex" -> _Complex , "Num" -> _?NumericQ, "NAN" -> _|>
 ]
Out[5]=

Replacement rules can also be labeled:

In[6]:=
ResourceFunction["GroupCases"][{1, 2, 3/2, 3., 1 + I, Pi, x},
 <|"Int" -> i_Integer :> i + Pi, "Frac" -> _Rational, "Float" -> _Real, "Complex" -> _Complex , "Num" -> _?NumericQ, "NAN" -> _|>
 ]
Out[6]=

GroupCases can be defined as an operator:

In[7]:=
splitNums = ResourceFunction["GroupCases"][
   <|"Int" -> _Integer, "Frac" -> _Rational, "Float" -> _Real, "Complex" -> _Complex , "Num" -> _?NumericQ, "NAN" -> _|>
   ];
splitNums@{1, 2, 3/2, 3., 1 + I, Pi, x}
Out[8]=

Properties and Relations (2) 

GroupCases always matches each element exactly once and all elements are returned under one of the keys in the output:

In[9]:=
list = {1, 2, 3/2, 3., 1 + I, Pi, x};
ResourceFunction["GroupCases"][list, {_Integer, _?NumericQ}]
Out[10]=

If no transformations are used, the following relation will hold:

In[11]:=
Sort[Join @@ Values[%]] === Sort[list]
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]:=
ResourceFunction["GroupCases"][{1, 2, 3}, {_Integer, _List}]
Out[12]=

Possible Issues (2) 

Matching a List at the first level of the input is only possible by specifying a List of patterns to match. The following will match every element in the input because of ambiguity:

In[13]:=
ResourceFunction["GroupCases"][{1, {}, {2}, 3}, {___}]
Out[13]=

Instead, use:

In[14]:=
ResourceFunction["GroupCases"][{1, {}, {2}, 3}, {{___}}]
Out[14]=

Publisher

Sjoerd Smit

Version History

  • 2.0.0 – 20 October 2020
  • 1.0.0 – 14 October 2020

Related Resources

License Information