Skip to content

Fold ​

Reduce a collection to one value, combining left to right from a seed.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageFold!
Fold(f, init, xs)f(…f(f(init,x1),x2)…,xn)f(\ldots f(f(init, x_1), x_2)\ldots, x_n) — initinit combined with each element of xsxs in turn.
Fold(f)the operator form, Fold(f)(xs)=Fold(f,xs)\mathrm{Fold}(f)(xs) = \mathrm{Fold}(f, xs).
Fold(f, xs)the same fold with no seed, starting from xsxs's own first element: f(…f(x1,x2)…,xn)f(\ldots f(x_1, x_2)\ldots, x_n).
Typed signatures
TypePackageApplies
(any) -> anycombinatoricsreplaces compute-enginealways
(reducer: (unknown, T) any -> unknown, initial: value, collection<T>) -> value where Tcompute-enginealways

Domain: Collections

Examples

Basic #
live ↗
InFold(Add,0,[1,2,3,4])\mathrm{Fold}(\mathrm{Add}, 0, \bigl\lbrack1, 2, 3, 4\bigr\rbrack)Out1010
live ↗
InFold(Multiply,1,[1,2,3,4])\mathrm{Fold}(\mathrm{Multiply}, 1, \bigl\lbrack1, 2, 3, 4\bigr\rbrack)Out2424

The shape of a left fold

live ↗
InFold(f,x,[a,b,c])\mathrm{Fold}(f, x, \bigl\lbrack a, b, c\bigr\rbrack)Outf(f(f(x,a),b),c)f(f(f(x, a), b), c)
live ↗
InFold(Max,0,[3,1,4,1,5])\mathrm{Fold}(\mathrm{Max}, 0, \bigl\lbrack3, 1, 4, 1, 5\bigr\rbrack)Out55

Shows the left-nested shape that a fold builds

live ↗
InReduce⁡([a,b,c,d],List,x)\operatorname{Reduce}\left(\bigl\lbrack a, b, c, d\bigr\rbrack, \mathrm{List}, x\right)Out[[[[x,a],b],c],d]\bigl\lbrack\bigl\lbrack\bigl\lbrack\bigl\lbrack x, a\bigr\rbrack, b\bigr\rbrack, c\bigr\rbrack, d\bigr\rbrack

A fold with an unknown ff makes the nesting visible

live ↗
InReduce⁡([a,b,c,d],f,x)\operatorname{Reduce}\left(\bigl\lbrack a, b, c, d\bigr\rbrack, f, x\right)Outf(f(f(f(x,a),b),c),d)f(f(f(f(x, a), b), c), d)

Folding Multiply with seed 1 is the product

live ↗
InReduce⁡([a,b,c,d],Multiply,1)\operatorname{Reduce}\left(\bigl\lbrack a, b, c, d\bigr\rbrack, \mathrm{Multiply}, 1\right)Outabcdabcd

Seeding with the first element folds over the rest

live ↗
InReduce⁡(Rest([a,b,c,d]),f,First([a,b,c,d]))\operatorname{Reduce}\left(\mathrm{Rest}(\bigl\lbrack a, b, c, d\bigr\rbrack), f, \mathrm{First}(\bigl\lbrack a, b, c, d\bigr\rbrack)\right)Outf(f(f(a,b),c),d)f(f(f(a, b), c), d)
Scope #

Symbolic elements

live ↗
InFold(Add,0,[a,b,c])\mathrm{Fold}(\mathrm{Add}, 0, \bigl\lbrack a, b, c\bigr\rbrack)Outa+b+ca+b+c

Any two-argument function, here one that nests pairs

live ↗
InFold((_1⁡,_2⁡)↦[_1⁡,_2⁡],x,[a,b])\mathrm{Fold}((\operatorname{\_1}, \operatorname{\_2})↦\bigl\lbrack\operatorname{\_1}, \operatorname{\_2}\bigr\rbrack, x, \bigl\lbrack a, b\bigr\rbrack)Out[[x,a],b]\bigl\lbrack\bigl\lbrack x, a\bigr\rbrack, b\bigr\rbrack

Without a seed, folding starts from the first element

live ↗
InFold(f,[a,b,c])\mathrm{Fold}(f, \bigl\lbrack a, b, c\bigr\rbrack)Outf(f(a,b),c)f(f(a, b), c)

A two-argument function scales each side differently as it nests

live ↗
InReduce⁡([a,b,c,d],(_1⁡,_2⁡)↦[2_1⁡,3_2⁡],x)\operatorname{Reduce}\left(\bigl\lbrack a, b, c, d\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\bigl\lbrack2\operatorname{\_1}, 3\operatorname{\_2}\bigr\rbrack, x\right)Out[[[[16x,24a],12b],6c],3d]\bigl\lbrack\bigl\lbrack\bigl\lbrack\bigl\lbrack16x, 24a\bigr\rbrack, 12b\bigr\rbrack, 6c\bigr\rbrack, 3d\bigr\rbrack

Composes a list of permutations onto a starting arrangement

live ↗
InReduce⁡([[1,4,3,2],[3,2,4,1]],Permute,[a,b,c,d])\operatorname{Reduce}\left(\bigl\lbrack\bigl\lbrack1, 4, 3, 2\bigr\rbrack, \bigl\lbrack3, 2, 4, 1\bigr\rbrack\bigr\rbrack, \mathrm{Permute}, \bigl\lbrack a, b, c, d\bigr\rbrack\right)Out[b,d,a,c]\bigl\lbrack b, d, a, c\bigr\rbrack

Power folds left, giving ((ab)c)d((a^b)^c)^d

live ↗
InReduce⁡(Rest([a,b,c,d]),Power,First([a,b,c,d]))\operatorname{Reduce}\left(\mathrm{Rest}(\bigl\lbrack a, b, c, d\bigr\rbrack), \mathrm{Power}, \mathrm{First}(\bigl\lbrack a, b, c, d\bigr\rbrack)\right)Out((ab)c)d((a^{b})^{c})^{d}
live ↗
InFold(f)⊲[a,b,c,d]\mathrm{Fold}(f)\lhd\bigl\lbrack a, b, c, d\bigr\rbrackOutf(f(f(a,b),c),d)f(f(f(a, b), c), d)
live ↗
InReduce⁡(Rest([[1,−1,1],[0,1,1],[1,1,−1]]),Cross,First([[1,−1,1],[0,1,1],[1,1,−1]]))\operatorname{Reduce}\left(\mathrm{Rest}(\bigl\lbrack\bigl\lbrack1, -1, 1\bigr\rbrack, \bigl\lbrack0, 1, 1\bigr\rbrack, \bigl\lbrack1, 1, -1\bigr\rbrack\bigr\rbrack), \mathrm{Cross}, \mathrm{First}(\bigl\lbrack\bigl\lbrack1, -1, 1\bigr\rbrack, \bigl\lbrack0, 1, 1\bigr\rbrack, \bigl\lbrack1, 1, -1\bigr\rbrack\bigr\rbrack)\right)Out[0,−1,−1]\bigl\lbrack0, -1, -1\bigr\rbrack
live ↗
InCatch(Fold((_1⁡,_2⁡)↦if _1⁡>106 then Throw(_1⁡) else _1⁡2+_2⁡,2,1..6))\mathrm{Catch}(\mathrm{Fold}((\operatorname{\_1}, \operatorname{\_2})↦\text{if }\operatorname{\_1}\gt10^6\text{ then }\mathrm{Throw}(\operatorname{\_1})\text{ else }\operatorname{\_1}^2+\operatorname{\_2}, 2, 1..6))Out287 111 645 589287\,111\,645\,589
Applications #

Assemble a number from its decimal digits

live ↗
InFold((_1⁡,_2⁡)↦10_1⁡+_2⁡,0,[1,2,3,4])\mathrm{Fold}((\operatorname{\_1}, \operatorname{\_2})↦10\operatorname{\_1}+\operatorname{\_2}, 0, \bigl\lbrack1, 2, 3, 4\bigr\rbrack)Out1 2341\,234

...or from its binary digits: 10112=111011_2 = 11

live ↗
InFold((_1⁡,_2⁡)↦2_1⁡+_2⁡,0,[1,0,1,1])\mathrm{Fold}((\operatorname{\_1}, \operatorname{\_2})↦2\operatorname{\_1}+\operatorname{\_2}, 0, \bigl\lbrack1, 0, 1, 1\bigr\rbrack)Out1111

Horner's rule for ax2+bx+cax^2 + bx + c

live ↗
InFold((_1⁡,_2⁡)↦x_1⁡+_2⁡,0,[a,b,c])\mathrm{Fold}((\operatorname{\_1}, \operatorname{\_2})↦x\operatorname{\_1}+\operatorname{\_2}, 0, \bigl\lbrack a, b, c\bigr\rbrack)Outx×(ax+b)+cx×(ax+b)+c

Evaluate the continued fraction [1;2,2,2][1; 2, 2, 2] from the inside out -- a convergent of 2\sqrt{2}

live ↗
InFold((_1⁡,_2⁡)↦_2⁡+1_1⁡,2,[2,2,1])\mathrm{Fold}((\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_2}+\frac{1}{\operatorname{\_1}}, 2, \bigl\lbrack2, 2, 1\bigr\rbrack)Out1712\frac{17}{12}

A continued fraction built from the reversed list

live ↗
InReduce⁡(Reverse([a,b,c,d]),(_1⁡,_2⁡)↦1_1⁡+_2⁡,x)\operatorname{Reduce}\left(\mathrm{Reverse}(\bigl\lbrack a, b, c, d\bigr\rbrack), (\operatorname{\_1}, \operatorname{\_2})↦\frac{1}{\operatorname{\_1}+\operatorname{\_2}}, x\right)Out1a+1b+1c+1d+x\frac{1}{a+\frac{1}{b+\frac{1}{c+\frac{1}{d+x}}}}

Digits to number: 10x+d10x+d at each step

live ↗
InReduce⁡([4,5,1,6,7,8],(_1⁡,_2⁡)↦10_1⁡+_2⁡,0)\operatorname{Reduce}\left(\bigl\lbrack4, 5, 1, 6, 7, 8\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦10\operatorname{\_1}+\operatorname{\_2}, 0\right)Out451 678451\,678

Swapping arguments nests to the right instead

live ↗
InReduce⁡([a,b,c,d],(_1⁡,_2⁡)↦[_2⁡,_1⁡],x)\operatorname{Reduce}\left(\bigl\lbrack a, b, c, d\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\bigl\lbrack\operatorname{\_2}, \operatorname{\_1}\bigr\rbrack, x\right)Out[d,[c,[b,[a,x]]]]\bigl\lbrack d, \bigl\lbrack c, \bigl\lbrack b, \bigl\lbrack a, x\bigr\rbrack\bigr\rbrack\bigr\rbrack\bigr\rbrack

Repeated partition builds a nested block structure

live ↗
InReduce⁡([2,4,3],Partition,1..30)\operatorname{Reduce}\left(\bigl\lbrack2, 4, 3\bigr\rbrack, \mathrm{Partition}, 1..30\right)Out[[[[1,2],[3,4],[5,6],[7,8]],[[9,10],[11,12],[13,14],[15,16]],[[17,18],[19,20],[21,22],[23,24]]]]\bigl\lbrack\bigl\lbrack\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3, 4\bigr\rbrack, \bigl\lbrack5, 6\bigr\rbrack, \bigl\lbrack7, 8\bigr\rbrack\bigr\rbrack, \bigl\lbrack\bigl\lbrack9, 10\bigr\rbrack, \bigl\lbrack11, 12\bigr\rbrack, \bigl\lbrack13, 14\bigr\rbrack, \bigl\lbrack15, 16\bigr\rbrack\bigr\rbrack, \bigl\lbrack\bigl\lbrack17, 18\bigr\rbrack, \bigl\lbrack19, 20\bigr\rbrack, \bigl\lbrack21, 22\bigr\rbrack, \bigl\lbrack23, 24\bigr\rbrack\bigr\rbrack\bigr\rbrack\bigr\rbrack

Horner's rule for a polynomial in xx

live ↗
InReduce⁡([a,b,c,d,k],(_1⁡,_2⁡)↦_1⁡x+_2⁡,0)\operatorname{Reduce}\left(\bigl\lbrack a, b, c, d, k\bigr\rbrack, (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_1}x+\operatorname{\_2}, 0\right)Outx×(x×(x×(ax+b)+c)+d)+kx×(x×(x×(ax+b)+c)+d)+k

An alternating sum from a fold with negation

live ↗
InReduce⁡(Reverse([a,b,c,d,k]),(_1⁡,_2⁡)↦_2⁡−_1⁡,0)\operatorname{Reduce}\left(\mathrm{Reverse}(\bigl\lbrack a, b, c, d, k\bigr\rbrack), (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{\_2}-\operatorname{\_1}, 0\right)Outa−b+c−d+ka-b+c-d+k
Properties #

Folding an empty list returns the seed

live ↗
InFold(f,x,[])\mathrm{Fold}(f, x, \bigl\lbrack\bigr\rbrack)Outxx

Folding with Add is a sum: 1+2+⋯+1001 + 2 + \cdots + 100

live ↗
InFold(Add,0,1..100)\mathrm{Fold}(\mathrm{Add}, 0, 1..100)Out5 0505\,050

Folding the reversed list reverses the nesting

live ↗
InReduce⁡(Reverse([a,b,c,d]),g,x)\operatorname{Reduce}\left(\mathrm{Reverse}(\bigl\lbrack a, b, c, d\bigr\rbrack), g, x\right)Outg(g(g(g(x,d),c),b),a)g(g(g(g(x, d), c), b), a)

Appending each element to an empty list rebuilds the list

live ↗
InReduce⁡([1,2,3,4,5],Append,[])\operatorname{Reduce}\left(\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack, \mathrm{Append}, \bigl\lbrack\bigr\rbrack\right)Out[1,2,3,4,5]\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack

A single element folds to itself, with no call to ff

live ↗
InReduce⁡(Rest([a]),f,First([a]))\operatorname{Reduce}\left(\mathrm{Rest}(\bigl\lbrack a\bigr\rbrack), f, \mathrm{First}(\bigl\lbrack a\bigr\rbrack)\right)Outaa

The general left fold: g(g(g(g(x,a),b),c),d)g(g(g(g(x,a),b),c),d)

live ↗
InReduce⁡([a,b,c,d],g,x)\operatorname{Reduce}\left(\bigl\lbrack a, b, c, d\bigr\rbrack, g, x\right)Outg(g(g(g(x,a),b),c),d)g(g(g(g(x, a), b), c), d)
Possible issues #
live ↗
InReduce⁡(Rest([]),f,First([]))\operatorname{Reduce}\left(\mathrm{Rest}(\bigl\lbrack\bigr\rbrack), f, \mathrm{First}(\bigl\lbrack\bigr\rbrack)\right)OutMissing\mathrm{Missing}
Neat examples #
live ↗
InReduce⁡([a,b,c],(s,k)↦Join(s,Map(_1⁡↦Append(_1⁡,k),s)),[[]])\operatorname{Reduce}\left(\bigl\lbrack a, b, c\bigr\rbrack, (s, k)↦\mathrm{Join}(s, \mathrm{Map}(\operatorname{\_1}↦\mathrm{Append}(\operatorname{\_1}, k), s)), \bigl\lbrack\bigl\lbrack\bigr\rbrack\bigr\rbrack\right)Out[[],[a],[b],[a,b],[c],[a,c],[b,c],[a,b,c]]\bigl\lbrack\bigl\lbrack\bigr\rbrack, \bigl\lbrack a\bigr\rbrack, \bigl\lbrack b\bigr\rbrack, \bigl\lbrack a, b\bigr\rbrack, \bigl\lbrack c\bigr\rbrack, \bigl\lbrack a, c\bigr\rbrack, \bigl\lbrack b, c\bigr\rbrack, \bigl\lbrack a, b, c\bigr\rbrack\bigr\rbrack
live ↗
InArray(_1⁡↦Reduce⁡(1..2.._1⁡,(_1⁡,_2⁡)↦Reduce⁡(1..2.._1⁡,(_1⁡,_2⁡)↦−_1⁡+(_2⁡+2)×2⌈log⁡2(⌈(_1⁡+2)/(_2⁡+2)⌉)⌉−2,_2⁡),0),100)\mathrm{Array}(\operatorname{\_1}↦\operatorname{Reduce}\left(1..2..\operatorname{\_1}, (\operatorname{\_1}, \operatorname{\_2})↦\operatorname{Reduce}\left(1..2..\operatorname{\_1}, (\operatorname{\_1}, \operatorname{\_2})↦-\operatorname{\_1}+(\operatorname{\_2}+2)×2^{\lceil\log_{2}(\lceil(\operatorname{\_1}+2)/(\operatorname{\_2}+2)\rceil)\rceil}-2, \operatorname{\_2}\right), 0\right), 100)Out[1,2,1,0,5,2,3,3,2,2,3,4,1,8,5,4,2,2,3,3,2,2,7,2,9,5,2,12,9,7,5,4,2,2,3,4,1,8,5,4,2,2,3,3,2,2,15,8,5,1,43,20,13,10,3,14,7,3,11,8,3,8,5,4,2,2,3,4,1,24,13,5,4,2,11,4,5,5,4,1,13,6,5,5,4,2,7,5,3,1,3,3,2,2,31,14,10,3,3,2]\bigl\lbrack1, 2, 1, 0, 5, 2, 3, 3, 2, 2, 3, 4, 1, 8, 5, 4, 2, 2, 3, 3, 2, 2, 7, 2, 9, 5, 2, 12, 9, 7, 5, 4, 2, 2, 3, 4, 1, 8, 5, 4, 2, 2, 3, 3, 2, 2, 15, 8, 5, 1, 43, 20, 13, 10, 3, 14, 7, 3, 11, 8, 3, 8, 5, 4, 2, 2, 3, 4, 1, 24, 13, 5, 4, 2, 11, 4, 5, 5, 4, 1, 13, 6, 5, 5, 4, 2, 7, 5, 3, 1, 3, 3, 2, 2, 31, 14, 10, 3, 3, 2\bigr\rbrack
About
Examples36
Checked againstWolfram Language 35 agree1 differmathlib4 6 agree1 error
CrosswalkWolfram Language

Implementation

mappedmathlib4packages/oracle/src/mappings.ts

A left fold from the seed, as ours, so f(acc, element). The seedless two-argument form is not bound.

See also: Scan