Function Repository Resource:

SplitByPatterns

Source Notebook

Split a list into sublists that match the same pattern from an ordered list of patterns

Contributed by: Ian Ford (Wolfram Research)

ResourceFunction["SplitByPatterns"][list,{patt1,patt2,}]

splits list into sublists consisting of runs of successive elements that match the same patti.

Details and Options

ResourceFunction["SplitByPatterns"][list,{patt1,patt2,}] uses the first patti that an element matches to determine whether to split.

Examples

Basic Examples (1) 

Split into runs of either integers or strings:

In[1]:=
ResourceFunction[
 "SplitByPatterns"][{1, "a", 2, 3, "b", "c", "d"}, {_Integer, _String}]
Out[1]=

Properties and Relations (2) 

The first pattern that matches an element is used:

In[2]:=
ResourceFunction[
 "SplitByPatterns"][{2, 4, 3, 6, 7}, {_Integer?EvenQ, _Integer}]
Out[2]=

If the patterns are reversed, the result may differ:

In[3]:=
ResourceFunction[
 "SplitByPatterns"][{2, 4, 3, 6, 7}, {_Integer, _Integer?EvenQ}]
Out[3]=

SplitByPatterns[list,{patt1,}] is equivalent to SplitByPatterns[list,{patt1,,_}]:

In[4]:=
ResourceFunction[
 "SplitByPatterns"][{1, "a", 2, 3, "b", "c", "d"}, {_Integer}]
Out[4]=

Version History

  • 1.0.0 – 17 October 2019

License Information