Wolfram Research

Function Repository Resource:

FoldIndexed

Source Notebook

Perform a folding operation while also parsing the index as the third argument

Contributed by: Sander Huisman

ResourceFunction["FoldIndexed"][f,x,{a,b,}]

gives the last element of FoldListIndexed[f,x,{a,b,…}].

ResourceFunction["FoldIndexed"][f,{a,b,c,}]

is equivalent to ResourceFunction["FoldIndexed"][f,First[a],Rest[a]].

ResourceFunction["FoldIndexed"][f]

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

Details and Options

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

Examples

Basic Examples

Basic usage:

In[1]:=
ResourceFunction["FoldIndexed"][f, x, {a, b, c}]
Out[1]=

Scope

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

In[2]:=
ResourceFunction["FoldIndexed"][f, {a, b, c, d}]
Out[2]=

Make an operator:

In[3]:=
op = ResourceFunction["FoldIndexed"][f]
Out[3]=

Feed a single list to the operator:

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

Feed an initializer and a list to the operator:

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

Applications

Form a continued fraction with increasing power:

In[6]:=
ResourceFunction["FoldIndexed"][1/(#1 + #2^#3) &, a, {b, c, d, e, f}]
Out[6]=

Form a continued fraction with an increasing numerator:

In[7]:=
ResourceFunction["FoldIndexed"][#3/(#1 + #2) &, a, {b, c, d, e, f}]
Out[7]=

Form the following product: :

In[8]:=
ResourceFunction["FoldIndexed"][#1 #2^#3 &, 1, {a, b, c, d, e}]
Out[8]=

Resource History

Related Resources

License Information