Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Take elements from a list until an element satisfies a test
ResourceFunction["TakeUntil"][list,test] takes elements from list until the first element that satisfies test. If no element satisfies test or if the element that satisfies the test is the last element in the list, it returns the entire list. |
Take all elements until an odd number is encountered:
In[1]:= | ![]() |
Out[1]= | ![]() |
Use a pure function to take all elements until the first element above 10:
In[2]:= | ![]() |
Out[2]= | ![]() |
Elements of the list can be any expression:
In[3]:= | ![]() |
Out[3]= | ![]() |
Use TakeUntil to take elements from a list until the first element that satisfies the given test is found:
In[4]:= | ![]() |
Out[4]= | ![]() |
By default, TakeUntil returns the entire list when no element satisfies the condition:
In[5]:= | ![]() |
Out[5]= | ![]() |
TakeUntil returns the entire list when the element that satisfies the test is the last one in the list:
In[6]:= | ![]() |
Out[6]= | ![]() |
Use TakeUntil on an expression with head f:
In[7]:= | ![]() |
Out[7]= | ![]() |
Find the digits of GoldenRatio up to and including the first occurrence of a 7:
In[8]:= | ![]() |
Out[8]= | ![]() |
When the test for TakeUntil is the logical negation of the test for TakeWhile, the result of TakeWhile is always contained within the result of TakeUntil. For example:
In[9]:= | ![]() |
Out[9]= | ![]() |
If the test function always returns False, TakeUntil will return the entire list. For example:
In[10]:= | ![]() |
Out[10]= | ![]() |
When using TakeUntil with an Association, it operates on the Values:
In[11]:= | ![]() |
Out[11]= | ![]() |
Use TakeUntil with the resource function SymbolQ:
In[12]:= | ![]() |
Out[12]= | ![]() |
Use TakeUntil with the resource function PossibleNameQ:
In[13]:= | ![]() |
Out[13]= | ![]() |
If the criterion does not return strictly boolean values (True or False), TakeUntil will treat any non-True value as False. For example:
In[14]:= | ![]() |
Out[14]= | ![]() |
Use TakeUntil on a list of integer pairs, with a condition to stop at the first pair where the total exceeds a specified threshold:
In[15]:= | ![]() |
In[16]:= | ![]() |
Out[16]= | ![]() |
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License