Wolfram Research

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

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

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];

Feed a list of lists:

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

Applications

First integrate x from 0 to 2, then y from 0 to 2, then 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

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

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

Resource History

Related Resources

License Information