Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Take elements before the specified element in an expression
ResourceFunction["TakeBefore"][list,pattern,n] takes n elements of list preceding the first occurrence of pattern. | |
ResourceFunction["TakeBefore"][list,pattern,n,levelspec] takes elements on levels specified by levelspec. | |
ResourceFunction["TakeBefore"][pattern,n,…] represents an operator form of ResourceFunction["TakeBefore"] that can be applied to an expression. |
Take the two elements before the element d in a list:
In[1]:= |
In[2]:= |
Out[2]= |
Do the same using the operator form of TakeBefore:
In[3]:= |
Out[3]= |
Take all elements before b:
In[4]:= |
Out[4]= |
Take elements from an expression with an arbitrary head:
In[5]:= |
Out[5]= |
Take as many elements as possible, up to 20:
In[6]:= |
Out[6]= |
Take the first three elements before the element e at level 2:
In[7]:= |
Out[7]= |
By default, TakeBefore does not include the head of an expression when creating a list of elements to match against the specified pattern:
In[8]:= |
Out[8]= |
Use the option Heads to include expression heads:
In[9]:= |
Out[9]= |
This work is licensed under a Creative Commons Attribution 4.0 International License