Function Repository Resource:

NestWhilePairList

Source Notebook

NestWhile analog of FoldPairList

Contributed by: Nik Murzin

ResourceFunction["NestWhilePairList"][f,y0,test]

gives the list of successive xi obtained by applying f to yi, until applying test to the yi no longer yields True where at each step f returns {xi,yi+1}.

ResourceFunction["NestWhilePairList"][f,y0,test,m]

supplies the most recent m results as arguments for test at each step.

ResourceFunction["NestWhilePairList"][f,y0,test,{mmin,m}]

does not start applying test until at least mmin results have been generated. At each step it then supplies as arguments to test as many recent results as possible, up to a maximum of m.

ResourceFunction["NestWhilePairList"][f,y0,test,All]

supplies all results so far as arguments for test at each step.

ResourceFunction["NestWhilePairList"][f,y0,test,m,max]

applies f at most max times.

ResourceFunction["NestWhilePairList"][f,y0,test,m,{min,max}]

returns a range of results spanning between min and up to max number of times applying f.

ResourceFunction["NestWhilePairList"][f,y0,test,m,minMax,n]

applies f an extra n times.

ResourceFunction["NestWhilePairList"][f,y0,test,m,minMax,-n]

returns the result found when f had been applied n fewer times.

ResourceFunction["NestWhilePairList"][f,y0,test,m,minMax,n,g]

gives the list of successive values of g[{xi,yi}].

Details

The last element of the list returned by ResourceFunction["NestWhilePairList"][f,y0,test] is the last xi such that test[yi] yields True.
In ResourceFunction["NestWhilePairList"][f,y0,test], the function f is never applied to yi for which test[yi] does not yield True.
The function f is expected to return a listed pair {x,y} of expressions.
If f[yi] does not return a pair or test[yi] does not yield True, then ResourceFunction["NestWhilePairList"][f,y0,test] returns {x0,x1,,xi-1}.
ResourceFunction["NestWhilePairList"][f,y0,test,m] does not start applying test until at least m results have been generated.
ResourceFunction["NestWhilePairList"][f,y0,test,m] at each step evaluates test[y0,y1,,ym-1].
The yi are given to test in the order they are generated, with the most recent coming last.
The min in ResourceFunction["NestWhilePairList"][f,y0,test,m,{min,max}] can be any integer, with negative integer counting back from the last step and zero referreing to y0.
ResourceFunction["NestWhilePairList"][f,y0,list,m,max,n] is equivalent to ResourceFunction["NestWhilePairList"][f,y0,list,m,max,n,First].

Examples

Basic Examples (3) 

Nest a function wrapping each outcome with another, while a given condition holds:

In[1]:=
ResourceFunction["NestWhilePairList"][Comap[{f, g}], x, Depth[#] < 5 &]
Out[1]=

Return results of applying a given function (e.g. Fibonacci) to an output of an iterating function (e.g. Prime):

In[2]:=
ResourceFunction["NestWhilePairList"][
 Comap[{Fibonacci, Prime}], 1, # <= 500 &]
Out[2]=

Compare:

In[3]:=
Fibonacci /@ Most@NestWhileList[Prime, 1, # <= 500 &]
Out[3]=

Compute intermediate quotients and remainders of the Euclidean algorithm for GCD[1071,462]:

In[4]:=
ResourceFunction["NestWhilePairList"][
 Apply[{q, r} |-> {{##}, {r, #2}} & @@ QuotientRemainder[q, r]], {1071, 462}, Apply[#2 > 0 &]]
Out[4]=

Scope (4) 

Provide more arguments to the test function:

In[5]:=
ResourceFunction["NestWhilePairList"][
 Comap[{Fibonacci, Prime}], 1, #2 - #1 < 100 &, 2]
Out[5]=

Iterate up to a given number of steps:

In[6]:=
ResourceFunction["NestWhilePairList"][
 Comap[{LeafCount, ResourceFunction["CombinatorStep"]}], CombinatorS[CombinatorS][CombinatorS][CombinatorS[CombinatorS]][
   CombinatorS][CombinatorS], True &, 1, 10]
Out[6]=

Return the range of steps:

In[7]:=
ResourceFunction["NestWhilePairList"][
 Comap[{LeafCount, ResourceFunction["CombinatorStep"]}], CombinatorS[CombinatorS][CombinatorS][CombinatorS[CombinatorS]][
   CombinatorS][CombinatorS], True &, 1, {5, 10}]
Out[7]=

Return last three elements:

In[8]:=
ResourceFunction["NestWhilePairList"][
 Comap[{LeafCount, ResourceFunction["CombinatorStep"]}], CombinatorS[CombinatorS][CombinatorS][CombinatorS[CombinatorS]][
   CombinatorS][CombinatorS], True &, 1, {-3, 10}]
Out[8]=

Include the initial expression:

In[9]:=
ResourceFunction["NestWhilePairList"][
 Comap[{LeafCount, ResourceFunction["CombinatorStep"]}], CombinatorS[CombinatorS][CombinatorS][CombinatorS[CombinatorS]][
   CombinatorS][CombinatorS], True &, 1, {0, 10}]
Out[9]=

Apply the function extra or fewer times relative to the point of test function succeeding:

In[10]:=
ResourceFunction["NestWhilePairList"][TakeDrop[#, UpTo[2]] &, Range[11], Length[#] > 1 &]
Out[10]=
In[11]:=
ResourceFunction["NestWhilePairList"][TakeDrop[#, UpTo[2]] &, Range[11], Length[#] > 1 &, 1, Infinity, 2]
Out[11]=
In[12]:=
ResourceFunction["NestWhilePairList"][TakeDrop[#, UpTo[2]] &, Range[11], Length[#] > 1 &, 1, Infinity, -2]
Out[12]=

Use last argument to get a result of applying any function to successive pairs:

In[13]:=
ResourceFunction["NestWhilePairList"][
 Comap[{Fibonacci, Prime}], 1, # < 100 &, 1, Infinity, 1, Identity]
Out[13]=

Applications (1) 

Evolve a combinator expression and return its plots:

In[14]:=
ResourceFunction[
 "NestWhilePairList"][{ResourceFunction["CombinatorPlot"][#, "FramedMatches"], ResourceFunction["CombinatorStep"][#]} &, CombinatorS[CombinatorS][CombinatorS][CombinatorS[CombinatorS]][
   CombinatorS][CombinatorS], LeafCount[#] < 50 &]
Out[14]=

Properties & Relations (1) 

The NestWhilePairList, unlike NestWhileList and FoldWhileList, does not include the last element for which the test fails:

In[15]:=
NestWhileList[#/2 &, 123456, EvenQ]
Out[15]=
In[16]:=
ResourceFunction["NestWhilePairList"][
 Comap[{Identity, #/2 &}], 123456, EvenQ]
Out[16]=
In[17]:=
FoldWhileList[Divide, 5!, Range[10], IntegerQ]
Out[17]=
In[18]:=
ResourceFunction["NestWhilePairList"][
 Apply[{#1, {#1/#2, #2 + 1}} &], {5!, 1}, IntegerQ@*First]
Out[18]=

Version History

  • 1.0.0 – 17 November 2025

Related Resources

License Information