Function Repository Resource:

FoldThread

Source Notebook

Perform a folding operation parsing multiple arguments

Contributed by: Sander Huisman

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

gives the last element of ResourceFunction["FoldThreadList"][f,x,{{a1,a2,},{b1,b2,},}].

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

gives ResourceFunction["FoldThread"][f,{a1,b1,},{{a2,},{b2,},}].

ResourceFunction["FoldThread"][f]

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

Details and Options

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

Examples

Basic Examples (1) 

Use FoldThread to fold a function over lists, threading the elements:

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

Scope (2) 

Without an initializer, the first element of each list is the initializer:

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

Make an operator:

In[3]:=
op = ResourceFunction["FoldThread"][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["FoldThread"][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[
 "FoldThread"][#3/(#1 + #2) &, 5, {{a, b, c, d}, {4, 3, 2, 1}}]
Out[6]=

Properties and Relations (1) 

Fold and FoldThread give equivalent results for the case in which a single argument is folded:

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

Publisher

SHuisman

Version History

  • 1.0.0 – 17 December 2019

Related Resources

License Information