Function Repository Resource:

FoldThreadList

Source Notebook

Perform a folding operation parsing multiple arguments

Contributed by: Sander Huisman

ResourceFunction["FoldThreadList"][f,x ,{{a1,a2,},{b1,b2,},}]

gives {x, f[x,a1,b1,…], f[f[x,a1,b2,…], a2,b1, …], …}.

ResourceFunction["FoldThreadList"][f,{{a1,a2,},{b1,b2,},}]

gives {{a1,b1,},f[{a1,b1,},a2,b2,],f[f[{a1,b1,},a2,b2,],a3,b3,],}.

ResourceFunction["FoldThreadList"][f]

represents an operator form of ResourceFunction["FoldThreadList"] that can be applied to expressions.

Details and Options

ResourceFunction["FoldThreadList"][f][list] is equivalent to ResourceFunction["FoldThreadList"][f,list].
ResourceFunction["FoldThreadList"][f][x,list] is equivalent to ResourceFunction["FoldThreadList"][f,x,list].

Examples

Basic Examples (1) 

Fold a function over lists, threading the elements:

In[1]:=
ResourceFunction[
 "FoldThreadList"][f, x, {{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}}]
Out[1]=

Scope (2) 

Without an initializer, the first elements of list is the initializer:

In[2]:=
ResourceFunction[
 "FoldThreadList"][f, {{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}}]
Out[2]=

Make an operator:

In[3]:=
op = ResourceFunction["FoldThreadList"][g];

Use the operator, feeding in a list of lists:

In[4]:=
op[start, {{a, b}, {c, d}}]
Out[4]=

Applications (2) 

Integrate the function f(x,y,z)=x+y+z as x ranges from 0 to 2, y from 0 to 2, and z from 0 to 2:

In[5]:=
ResourceFunction["FoldThreadList"][Integrate[#1, {#2, #3, #4}] &, x + y + z, {{x, y, z}, {0, 0, 0}, {2, 2, 2}}]
Out[5]=

Create a continued fraction with different numerators:

In[6]:=
ResourceFunction[
 "FoldThreadList"][#3/(#1 + #2) &, 5, {{a, b, c, d}, {4, 3, 2, 1}}]
Out[6]=

Properties and Relations (1) 

FoldList and FoldThreadList are related for the case a single argument is folded in:

In[7]:=
ResourceFunction["FoldThreadList"][f, x, {{x, y, z}}] == FoldList[f, x, {x, y, z}]
Out[7]=

Publisher

SHuisman

Version History

  • 1.0.0 – 25 November 2019

Related Resources

License Information