Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Drop elements at the end of a list so long as a criterion is fulfilled
ResourceFunction["DropTrailingWhile"][list,crit] drops elements ei from the end of list, continuing so long as crit[ei] is True. | |
ResourceFunction["DropTrailingWhile"][crit] represents an operator form of ResourceFunction["DropTrailingWhile"] that can be applied to expressions. |
Drop elements from the back that are odd:
In[1]:= |
Out[1]= |
Use a pure function to drop elements from the back while they are larger than 100:
In[2]:= |
Out[2]= |
Elements of the list can be any expression:
In[3]:= |
Out[3]= |
Create an operator of DropTrailingWhile that drops trailing even numbers:
In[4]:= |
Out[4]= |
Use the operator:
In[5]:= |
Out[5]= |
Use on an expression with head f:
In[6]:= |
Out[6]= |
DropTrailingWhile also works on SparseArray:
In[7]:= |
Out[7]= |
Drop trailing zeros:
In[8]:= |
Out[8]= |
DropTrailingWhile is equivalent to a combination of Drop, Reverse and LengthWhile:
In[9]:= |
Out[9]= |
In[10]:= |
Out[10]= |
In[11]:= |
Out[11]= |
DropTrailingWhile gives an empty list if crit[ei] is True for all elements:
In[12]:= |
Out[12]= |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License