Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
A function like SplitBy, but with added support for using the indices of the elements in the list
ResourceFunction["SplitByIndexed"][list,f] splits list into sublists consisting of runs of successive elements that give the same value when the function f is applied to the element and index. | |
ResourceFunction["SplitByIndexed"][list,{f1,f2,…}] recursively splits list into sublists by testing elements successively with each of the fi. | |
ResourceFunction["SplitByIndexed"][f] represents an operator form of ResourceFunction["SplitByIndexed"] that can be applied to expressions. |
Split the list into runs of length up to 3:
In[1]:= |
Out[1]= |
Single out the elements at positions that are a multiple of 4:
In[2]:= |
Out[2]= |
Split based on both the value and position in the list:
In[3]:= |
Out[3]= |
This might clarify:
In[4]:= |
Out[4]= |
Specify a second splitting function to partition the sublists into runs of length up to 2:
In[5]:= |
Out[5]= |
SplitByIndexed can be used in the same way as SplitBy if you restrict the function to only the first argument:
In[6]:= |
Out[6]= |
In[7]:= |
Out[7]= |
However, SplitByIndexed will evaluate the test function only exactly once for each element:
In[8]:= |
Out[8]= |
SplitBy evaluates the test function twice for all elements except the first and last:
In[9]:= |
Out[9]= |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License