<s>
In	O
functional	B-Language
programming	I-Language
,	O
fold	O
(	O
also	O
termed	O
reduce	O
,	O
accumulate	B-Application
,	O
aggregate	O
,	O
compress	O
,	O
or	O
inject	O
)	O
refers	O
to	O
a	O
family	O
of	O
higher-order	B-Language
functions	I-Language
that	O
analyze	O
a	O
recursive	O
data	B-General_Concept
structure	I-General_Concept
and	O
through	O
use	O
of	O
a	O
given	O
combining	O
operation	O
,	O
recombine	O
the	O
results	O
of	O
recursively	O
processing	O
its	O
constituent	O
parts	O
,	O
building	O
up	O
a	O
return	O
value	O
.	O
</s>
<s>
Typically	O
,	O
a	O
fold	O
is	O
presented	O
with	O
a	O
combining	O
function	O
,	O
a	O
top	O
node	B-Data_Structure
of	O
a	O
data	B-General_Concept
structure	I-General_Concept
,	O
and	O
possibly	O
some	O
default	O
values	O
to	O
be	O
used	O
under	O
certain	O
conditions	O
.	O
</s>
<s>
The	O
fold	O
then	O
proceeds	O
to	O
combine	O
elements	O
of	O
the	O
data	B-General_Concept
structure	I-General_Concept
's	O
hierarchy	O
,	O
using	O
the	O
function	O
in	O
a	O
systematic	O
way	O
.	O
</s>
<s>
Folds	O
are	O
in	O
a	O
sense	O
dual	O
to	O
unfolds	B-Application
,	O
which	O
take	O
a	O
seed	O
value	O
and	O
apply	O
a	O
function	O
corecursively	B-Application
to	O
decide	O
how	O
to	O
progressively	O
construct	O
a	O
corecursive	B-Application
data	B-General_Concept
structure	I-General_Concept
,	O
whereas	O
a	O
fold	O
recursively	O
breaks	O
that	O
structure	O
down	O
,	O
replacing	O
it	O
with	O
the	O
results	O
of	O
applying	O
a	O
combining	O
function	O
at	O
each	O
node	B-Data_Structure
on	O
its	O
terminal	B-Algorithm
values	O
and	O
the	O
recursive	O
results	O
(	O
catamorphism	B-Application
,	O
versus	O
anamorphism	B-Application
of	O
unfolds	B-Application
)	O
.	O
</s>
<s>
Folds	O
can	O
be	O
regarded	O
as	O
consistently	O
replacing	O
the	O
structural	O
components	O
of	O
a	O
data	B-General_Concept
structure	I-General_Concept
with	O
functions	O
and	O
values	O
.	O
</s>
<s>
Lists	O
,	O
for	O
example	O
,	O
are	O
built	O
up	O
in	O
many	O
functional	B-Language
languages	I-Language
from	O
two	O
primitives	O
:	O
any	O
list	O
is	O
either	O
an	O
empty	O
list	O
,	O
commonly	O
called	O
nil	O
( []	O
)	O
,	O
or	O
is	O
constructed	O
by	O
prefixing	O
an	O
element	O
in	O
front	O
of	O
another	O
list	O
,	O
creating	O
what	O
is	O
called	O
a	O
cons	B-Protocol
node	B-Data_Structure
(	O
Cons( X1	O
,	O
Cons( X2	O
,	O
Cons( 	O
...	O
(	O
Cons(Xn,nil)	O
)	O
)	O
)	O
)	O
)	O
,	O
resulting	O
from	O
application	O
of	O
a	O
cons	B-Protocol
function	O
(	O
written	O
down	O
as	O
a	O
colon	O
(	O
:	O
)	O
in	O
Haskell	B-Language
)	O
.	O
</s>
<s>
One	O
can	O
view	O
a	O
fold	O
on	O
lists	O
as	O
replacing	O
the	O
nil	O
at	O
the	O
end	O
of	O
the	O
list	O
with	O
a	O
specific	O
value	O
,	O
and	O
replacing	O
each	O
cons	B-Protocol
with	O
a	O
specific	O
function	O
.	O
</s>
<s>
There	O
's	O
another	O
way	O
to	O
perform	O
the	O
structural	O
transformation	O
in	O
a	O
consistent	O
manner	O
,	O
with	O
the	O
order	O
of	O
the	O
two	O
links	O
of	O
each	O
node	B-Data_Structure
flipped	O
when	O
fed	O
into	O
the	O
combining	O
function	O
:	O
</s>
<s>
These	O
pictures	O
illustrate	O
right	O
and	O
left	B-Application
fold	I-Application
of	O
a	O
list	O
visually	O
.	O
</s>
<s>
They	O
also	O
highlight	O
the	O
fact	O
that	O
foldr	B-Application
(	O
:	O
)	O
 [  ] 	O
is	O
the	O
identity	O
function	O
on	O
lists	O
(	O
a	O
shallow	O
copy	O
in	O
Lisp	B-Language
parlance	O
)	O
,	O
as	O
replacing	O
cons	B-Protocol
with	O
cons	B-Protocol
and	O
nil	O
with	O
nil	O
will	O
not	O
change	O
the	O
result	O
.	O
</s>
<s>
The	O
left	B-Application
fold	I-Application
diagram	O
suggests	O
an	O
easy	O
way	O
to	O
reverse	O
a	O
list	O
,	O
foldl	B-Application
(	O
flip	O
(	O
:	O
)	O
)	O
 [  ] 	O
.	O
</s>
<s>
Note	O
that	O
the	O
parameters	O
to	O
cons	B-Protocol
must	O
be	O
flipped	O
,	O
because	O
the	O
element	O
to	O
add	O
is	O
now	O
the	O
right	O
hand	O
parameter	O
of	O
the	O
combining	O
function	O
.	O
</s>
<s>
Another	O
easy	O
result	O
to	O
see	O
from	O
this	O
vantage-point	O
is	O
to	O
write	O
the	O
higher-order	O
map	O
function	O
in	O
terms	O
of	O
foldr	B-Application
,	O
by	O
composing	O
the	O
function	O
to	O
act	O
on	O
the	O
elements	O
with	O
cons	B-Protocol
,	O
as	O
:	O
</s>
<s>
is	O
an	O
operator	O
denoting	O
function	B-Application
composition	I-Application
.	O
</s>
<s>
Such	O
a	O
function	O
is	O
generally	O
referred	O
to	O
as	O
a	O
catamorphism	B-Application
.	O
</s>
<s>
On	O
lists	O
,	O
there	O
are	O
two	O
obvious	O
ways	O
to	O
carry	O
this	O
out	O
:	O
either	O
by	O
combining	O
the	O
first	O
element	O
with	O
the	O
result	O
of	O
recursively	O
combining	O
the	O
rest	O
(	O
called	O
a	O
right	B-Application
fold	I-Application
)	O
,	O
or	O
by	O
combining	O
the	O
result	O
of	O
recursively	O
combining	O
all	O
elements	O
but	O
the	O
last	O
one	O
,	O
with	O
the	O
last	O
element	O
(	O
called	O
a	O
left	B-Application
fold	I-Application
)	O
.	O
</s>
<s>
This	O
corresponds	O
to	O
a	O
binary	O
operator	O
being	O
either	O
right-associative	O
or	O
left-associative	O
,	O
in	O
Haskell	B-Language
's	O
or	O
Prolog	B-Language
's	O
terminology	O
.	O
</s>
<s>
With	O
a	O
right	B-Application
fold	I-Application
,	O
the	O
sum	O
would	O
be	O
parenthesized	O
as	O
1	O
+	O
(	O
2	O
+	O
(	O
3	O
+	O
(	O
4	O
+	O
5	O
)	O
)	O
)	O
,	O
whereas	O
with	O
a	O
left	B-Application
fold	I-Application
it	O
would	O
be	O
parenthesized	O
as	O
( ( ( 	O
1	O
+	O
2	O
)	O
+	O
3	O
)	O
+	O
4	O
)	O
+	O
5	O
.	O
</s>
<s>
In	O
practice	O
,	O
it	O
is	O
convenient	O
and	O
natural	O
to	O
have	O
an	O
initial	O
value	O
which	O
in	O
the	O
case	O
of	O
a	O
right	B-Application
fold	I-Application
is	O
used	O
when	O
one	O
reaches	O
the	O
end	O
of	O
the	O
list	O
,	O
and	O
in	O
the	O
case	O
of	O
a	O
left	B-Application
fold	I-Application
is	O
what	O
is	O
initially	O
combined	O
with	O
the	O
first	O
element	O
of	O
the	O
list	O
.	O
</s>
<s>
In	O
the	O
example	O
above	O
,	O
the	O
value	O
0	O
(	O
the	O
additive	O
identity	O
)	O
would	O
be	O
chosen	O
as	O
an	O
initial	O
value	O
,	O
giving	O
1	O
+	O
(	O
2	O
+	O
(	O
3	O
+	O
(	O
4	O
+	O
(	O
5	O
+	O
0	O
)	O
)	O
)	O
)	O
for	O
the	O
right	B-Application
fold	I-Application
,	O
and	O
( ( ( ( 	O
0	O
+	O
1	O
)	O
+	O
2	O
)	O
+	O
3	O
)	O
+	O
4	O
)	O
+	O
5	O
for	O
the	O
left	B-Application
fold	I-Application
.	O
</s>
<s>
Whereas	O
linear	O
folds	O
are	O
node-oriented	O
and	O
operate	O
in	O
a	O
consistent	O
manner	O
for	O
each	O
node	B-Data_Structure
of	O
a	O
list	O
,	O
tree-like	O
folds	O
are	O
whole-list	O
oriented	O
and	O
operate	O
in	O
a	O
consistent	O
manner	O
across	O
groups	O
of	O
nodes	O
.	O
</s>
<s>
When	O
no	O
initial	O
value	O
seems	O
appropriate	O
,	O
for	O
example	O
,	O
when	O
one	O
wants	O
to	O
fold	O
the	O
function	O
which	O
computes	O
the	O
maximum	O
of	O
its	O
two	O
parameters	O
over	O
a	O
non-empty	O
list	O
to	O
get	O
the	O
maximum	O
element	O
of	O
the	O
list	O
,	O
there	O
are	O
variants	O
of	O
foldr	B-Application
and	O
foldl	B-Application
which	O
use	O
the	O
last	O
and	O
first	O
element	O
of	O
the	O
list	O
respectively	O
as	O
the	O
initial	O
value	O
.	O
</s>
<s>
In	O
Haskell	B-Language
and	O
several	O
other	O
languages	O
,	O
these	O
are	O
called	O
foldr1	O
and	O
foldl1	O
,	O
the	O
1	O
making	O
reference	O
to	O
the	O
automatic	O
provision	O
of	O
an	O
initial	O
element	O
,	O
and	O
the	O
fact	O
that	O
the	O
lists	O
they	O
are	O
applied	O
to	O
must	O
have	O
at	O
least	O
one	O
element	O
.	O
</s>
<s>
Richard	O
Bird	O
in	O
his	O
2010	O
book	O
proposes	O
"	O
a	O
general	O
fold	B-Application
function	I-Application
on	O
non-empty	O
lists	O
"	O
foldrn	O
which	O
transforms	O
its	O
last	O
element	O
,	O
by	O
applying	O
an	O
additional	O
argument	O
function	O
to	O
it	O
,	O
into	O
a	O
value	O
of	O
the	O
result	O
type	O
before	O
starting	O
the	O
folding	O
itself	O
,	O
and	O
is	O
thus	O
able	O
to	O
use	O
type-asymmetrical	O
binary	O
operation	O
like	O
the	O
regular	O
foldr	B-Application
to	O
produce	O
a	O
result	O
of	O
type	O
different	O
from	O
the	O
list	O
's	O
elements	O
type	O
.	O
</s>
<s>
Using	O
Haskell	B-Language
as	O
an	O
example	O
,	O
foldl	B-Application
and	O
foldr	B-Application
can	O
be	O
formulated	O
in	O
a	O
few	O
equations	O
.	O
</s>
<s>
In	O
the	O
presence	O
of	O
lazy	O
,	O
or	O
non-strict	O
evaluation	O
,	O
foldr	B-Application
will	O
immediately	O
return	O
the	O
application	O
of	O
f	O
to	O
the	O
head	O
of	O
the	O
list	O
and	O
the	O
recursive	O
case	O
of	O
folding	O
over	O
the	O
rest	O
of	O
the	O
list	O
.	O
</s>
<s>
This	O
allows	O
right	B-Application
folds	I-Application
to	O
operate	O
on	O
infinite	O
lists	O
.	O
</s>
<s>
By	O
contrast	O
,	O
foldl	B-Application
will	O
immediately	O
call	O
itself	O
with	O
new	O
parameters	O
until	O
it	O
reaches	O
the	O
end	O
of	O
the	O
list	O
.	O
</s>
<s>
This	O
tail	B-Language
recursion	I-Language
can	O
be	O
efficiently	O
compiled	O
as	O
a	O
loop	O
,	O
but	O
ca	O
n't	O
deal	O
with	O
infinite	O
lists	O
at	O
all	O
—	O
it	O
will	O
recurse	O
forever	O
in	O
an	O
infinite	B-Algorithm
loop	I-Algorithm
.	O
</s>
<s>
Having	O
reached	O
the	O
end	O
of	O
the	O
list	O
,	O
an	O
expression	O
is	O
in	O
effect	O
built	O
by	O
foldl	B-Application
of	O
nested	O
left-deepening	O
f-applications	O
,	O
which	O
is	O
then	O
presented	O
to	O
the	O
caller	O
to	O
be	O
evaluated	O
.	O
</s>
<s>
This	O
means	O
that	O
while	O
foldr	B-Application
recurses	O
on	O
the	O
right	O
,	O
it	O
allows	O
for	O
a	O
lazy	O
combining	O
function	O
to	O
inspect	O
list	O
's	O
elements	O
from	O
the	O
left	O
;	O
and	O
conversely	O
,	O
while	O
foldl	B-Application
recurses	O
on	O
the	O
left	O
,	O
it	O
allows	O
for	O
a	O
lazy	O
combining	O
function	O
to	O
inspect	O
list	O
's	O
elements	O
from	O
the	O
right	O
,	O
if	O
it	O
so	O
chooses	O
(	O
e.g.	O
,	O
)	O
.	O
</s>
<s>
Reversing	O
a	O
list	O
is	O
also	O
tail-recursive	B-Language
(	O
it	O
can	O
be	O
implemented	O
using	O
)	O
.	O
</s>
<s>
On	O
finite	O
lists	O
,	O
that	O
means	O
that	O
left-fold	O
and	O
reverse	O
can	O
be	O
composed	O
to	O
perform	O
a	O
right	B-Application
fold	I-Application
in	O
a	O
tail-recursive	B-Language
way	O
(	O
cf	O
.	O
</s>
<s>
The	O
extraneous	O
intermediate	O
list	O
structure	O
can	O
be	O
eliminated	O
with	O
the	O
continuation-passing	B-Application
style	I-Application
technique	O
,	O
;	O
similarly	O
,	O
(	O
flip	O
is	O
only	O
needed	O
in	O
languages	O
like	O
Haskell	B-Language
with	O
its	O
flipped	O
order	O
of	O
arguments	O
to	O
the	O
combining	O
function	O
of	O
foldl	B-Application
unlike	O
e.g.	O
,	O
in	O
Scheme	B-Language
where	O
the	O
same	O
order	O
of	O
arguments	O
is	O
used	O
for	O
combining	O
functions	O
to	O
both	O
foldl	B-Application
and	O
)	O
.	O
</s>
<s>
Another	O
technical	O
point	O
is	O
that	O
,	O
in	O
the	O
case	O
of	O
left	B-Application
folds	I-Application
using	O
lazy	O
evaluation	O
,	O
the	O
new	O
initial	O
parameter	O
is	O
not	O
being	O
evaluated	O
before	O
the	O
recursive	O
call	O
is	O
made	O
.	O
</s>
<s>
In	O
Haskell	B-Language
this	O
is	O
the	O
foldl	B-Application
 '	O
(	O
note	O
the	O
apostrophe	O
,	O
pronounced	O
'	O
prime	O
 '	O
)	O
function	O
in	O
the	O
Data.List	O
library	O
(	O
one	O
needs	O
to	O
be	O
aware	O
of	O
the	O
fact	O
though	O
that	O
forcing	O
a	O
value	O
built	O
with	O
a	O
lazy	O
data	O
constructor	O
wo	O
n't	O
force	O
its	O
constituents	O
automatically	O
by	O
itself	O
)	O
.	O
</s>
<s>
Combined	O
with	O
tail	B-Language
recursion	I-Language
,	O
such	O
folds	O
approach	O
the	O
efficiency	O
of	O
loops	O
,	O
ensuring	O
constant	O
space	O
operation	O
,	O
when	O
lazy	O
evaluation	O
of	O
the	O
final	O
result	O
is	O
impossible	O
or	O
undesirable	O
.	O
</s>
<s>
Using	O
a	O
Haskell	B-Language
interpreter	O
,	O
the	O
structural	O
transformations	O
which	O
fold	B-Application
functions	I-Application
perform	O
can	O
be	O
illustrated	O
by	O
constructing	O
a	O
string	O
:	O
</s>
<s>
Infinite	O
tree-like	O
folding	O
is	O
demonstrated	O
e.g.	O
,	O
in	O
recursive	O
primes	O
production	O
by	O
unbounded	O
sieve	O
of	O
Eratosthenes	O
in	O
Haskell	B-Language
:	O
</s>
<s>
Language	O
Left	B-Application
fold	I-Application
Right	B-Application
fold	I-Application
Left	B-Application
fold	I-Application
without	O
initial	O
value	O
Right	B-Application
fold	I-Application
without	O
initial	O
value	O
Unfold	B-Application
Notes	O
APL	B-Language
func⍨	O
/	O
⌽initval	O
,	O
vector	O
func/vector	O
,	O
initval	O
func⍨	O
/	O
⌽vector	O
func/vector	O
C#	B-Application
3.0	B-Language
ienum.Aggregate(initval, func )	O
ienum.Reverse( )	O
.Aggregate(initval, func )	O
ienum.Aggregate(func )	O
ienum.Reverse( )	O
.Aggregate(func )	O
Aggregate	O
is	O
an	O
extension	O
method	O
ienum	O
is	O
an	O
IEnumerablexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
Similarly	O
in	O
all	O
.NET	O
languages	O
C++	B-Language
std::accumulate(begin, end, initval, func )	O
std::accumulate(rbegin, rend, initval, func )	O
in	O
header	O
<	O
numeric>begin	O
,	O
end	O
,	O
rbegin	O
,	O
rend	O
are	O
iteratorsfunc	O
can	O
be	O
a	O
function	B-Language
pointer	I-Language
or	O
a	O
function	B-Language
object	I-Language
C++17	B-Language
(	O
initval	O
op	O
...	O
op	O
pack	O
)	O
(	O
pack	O
op	O
...	O
op	O
initval	O
)	O
(	O
...	O
op	O
pack	O
)	O
(	O
pack	O
op	O
...	O
)	O
Fold	O
expression	O
(	O
only	O
for	O
variadic	B-Language
templates	I-Language
)	O
:	O
op	O
is	O
a	O
binary	O
operator	O
(	O
both	O
ops	O
must	O
be	O
the	O
same	O
,	O
e.g.	O
,	O
)	O
,	O
pack	O
is	O
an	O
unexpanded	O
parameter	O
pack	O
.	O
</s>
<s>
C++23	B-Language
std::ranges::fold_left(range, initval, func )	O
std::ranges::fold_right(range, initval, func )	O
std::ranges::fold_left_first(range, func )	O
std::ranges::fold_right_last(range, func )	O
Both	O
std::ranges::fold_left_first	O
and	O
std::ranges::fold_right_last	O
return	O
std::optional	O
considering	O
the	O
emptiness	O
of	O
range	O
.	O
</s>
<s>
CFML	B-Language
obj.reduce(func,initial )	O
obj.reduce(func )	O
Where	O
func	O
receives	O
as	O
arguments	O
the	O
result	O
of	O
the	O
previous	O
operation	O
(	O
or	O
the	O
initial	O
value	O
on	O
the	O
first	O
iteration	O
)	O
;	O
the	O
current	O
item	O
;	O
the	O
current	O
item	O
's	O
index	O
or	O
key	O
;	O
and	O
a	O
reference	O
to	O
the	O
obj	O
Clojure	B-Language
(	O
reduce	O
func	O
initval	O
list	O
)	O
(	O
reduce	O
func	O
initval	O
(	O
reverse	O
list	O
)	O
)	O
(	O
reduce	O
func	O
list	O
)	O
(	O
reduce	O
func	O
(	O
reverse	O
list	O
)	O
)	O
See	O
also	O
clojure.core.reducers/fold	O
Common	B-Language
Lisp	I-Language
(	O
reduce	O
func	O
list	O
:initial-value	O
initval	O
)	O
(	O
reduce	O
func	O
list	O
:from-end	O
t	O
:initial-value	O
initval	O
)	O
(	O
reduce	O
func	O
list	O
)	O
(	O
reduce	O
func	O
list	O
:from-end	O
t	O
)	O
Curl	B-Language
{{	O
TreeNode.default	O
treeNode	O
...	O
}	O
.to-Iterator	O
}	O
{{	O
TreeNode.default	O
treeNode	O
...	O
}	O
.reverse	O
}	O
.	O
}	O
</s>
<s>
do}	O
also	O
DefaultListModel	O
and	O
HashTable	O
implement	O
to-Iterator	O
D	B-Application
reduce	O
!	O
func(initval, list )	O
reduce	O
!	O
func( initval	O
,	O
list.reverse	O
)	O
reduce	O
!	O
func(list )	O
reduce	O
!	O
func( 	O
list.reverse	O
)	O
in	O
module	O
std.algorithm	O
Elixir	B-Language
List.foldl(list, acc, fun )	O
List.foldr(list, acc, fun )	O
See	O
documentation	O
for	O
example	O
usage	O
Elm	B-Language
List.foldl(Fun, Accumulator, List )	O
List.foldr(Fun, Accumulator, List )	O
See	O
also	O
List	O
API	O
Erlang	B-Operating_System
lists:foldl(Fun, Accumulator, List )	O
lists:foldr(Fun, Accumulator, List )	O
F#	B-Operating_System
Seq/List.fold	O
func	O
initval	O
list	O
List.foldBack	O
func	O
list	O
initval	O
Seq/List.reduce	O
func	O
list	O
List.reduceBack	O
func	O
list	O
Seq.unfold	O
func	O
initval	O
Gosu	B-Application
Iterable.fold(f(agg, e )	O
)	O
Iterable.reduce	O
( init	O
,	O
f(agg, e )	O
)	O
Iterable.partition(f(e )	O
)	O
All	O
are	O
extension	O
methods	O
on	O
Java	B-Language
's	O
Iterable	O
interface	O
,	O
arrays	O
are	O
also	O
supported	O
Groovy	B-Application
list.inject(initval, func )	O
list.reverse( )	O
.inject(initval, func )	O
list.inject(func )	O
list.reverse( )	O
.inject(func )	O
Haskell	B-Language
foldl	B-Application
func	O
initval	O
list	O
foldr	B-Application
func	O
initval	O
list	O
foldl1	O
func	O
list	O
foldr1	O
func	O
list	O
unfoldr	O
func	O
initval	O
For	O
foldl	B-Application
,	O
the	O
folding	O
function	O
takes	O
arguments	O
in	O
the	O
opposite	O
order	O
as	O
that	O
for	O
foldr	B-Application
.	O
</s>
<s>
Haxe	B-Language
Lambda.fold(iterable, func, initval )	O
J	B-Language
verb	O
~	O
/	O
|	O
.	O
</s>
<s>
"	O
J	B-Language
Dictionary	O
:	O
Insert	O
"	O
Java	B-Language
8+	O
stream.reduce(initval, func )	O
stream.reduce(func )	O
JavaScript	B-Language
1.8ECMAScript	O
5	O
array.reduce(func, initval )	O
array.reduce(func )	O
Julia	B-Application
foldl( op	O
,	O
itr	O
;	O
 [ init ] 	O
)	O
foldr( op	O
,	O
itr	O
;	O
 [ init ] 	O
)	O
foldl(op, itr )	O
foldr(op, itr )	O
Kotlin	B-Language
Iterable.fold(initval, func )	O
Iterable.foldRight(initval, func )	O
Iterable.reduce(func )	O
Iterable.reduceRight(func )	O
Other	O
collections	O
also	O
support	O
fold	O
and	O
reduce	O
.	O
</s>
<s>
LFE	B-Language
(	O
lists:foldl	O
func	O
accum	O
list	O
)	O
(	O
lists:foldr	O
func	O
accum	O
list	O
)	O
Logtalk	B-Language
fold_left(Closure, Initial, List, Result )	O
fold_right(Closure, Initial, List, Result )	O
Meta-predicates	O
provided	O
by	O
the	O
meta	O
standard	O
library	O
object	O
.	O
</s>
<s>
The	O
abbreviations	O
foldl	B-Application
and	O
foldr	B-Application
may	O
also	O
be	O
used	O
.	O
</s>
<s>
</s>
<s>
MATLAB	B-Language
fold(@func, list, defaultVal )	O
fold( 	O
@func	O
,	O
flip(list )	O
,	O
defaultVal	O
)	O
fold(@func, list )	O
fold( 	O
@func	O
,	O
flip(list )	O
)	O
Requires	O
Symbolic	O
Math	O
Toolbox	O
,	O
supported	O
from	O
R2016b	O
.	O
</s>
<s>
Maxima	B-Language
lreduce(func, list, initval )	O
rreduce(func, list, initval )	O
lreduce(func, list )	O
rreduce(func, list )	O
Mythryl	O
fold_left	O
func	O
initval	O
list	O
vector::fold_left	O
func	O
initval	O
vector	O
fold_right	O
func	O
initval	O
list	O
vector::fold_right	O
func	O
initval	O
vector	O
The	O
supplied	O
function	O
takes	O
its	O
arguments	O
in	O
a	O
tuple	O
.	O
</s>
<s>
OCaml	B-Language
List.fold_left	O
func	O
initval	O
list	O
Array.fold_left	O
func	O
initval	O
array	O
List.fold_right	O
func	O
list	O
initval	O
Array.fold_right	O
func	O
array	O
initval	O
Base.Sequence.unfold	O
~	O
init	O
~	O
f	O
Oz	B-Language
{	O
FoldL	B-Application
List	O
Func	O
InitVal}	O
{	O
FoldR	B-Application
List	O
Func	O
InitVal}	O
PARI/GP	B-Language
fold( f, A )	O
Perl	B-Language
reduce	O
block	O
initval	O
,	O
list	O
reduce	O
block	O
list	O
in	O
List::Util	O
module	O
PHP	B-Application
array_reduce(array, func, initval )	O
array_reduce(array_reverse(array )	O
,	O
func	O
,	O
initval	O
)	O
array_reduce(array, func )	O
array_reduce(array_reverse(array )	O
,	O
func	O
)	O
When	O
initval	O
is	O
not	O
supplied	O
,	O
NULL	O
is	O
used	O
,	O
so	O
this	O
is	O
not	O
a	O
true	O
foldl1	O
.	O
</s>
<s>
Before	O
PHP	B-Application
5.3	O
,	O
initval	O
can	O
only	O
be	O
integer	O
.	O
</s>
<s>
</s>
<s>
Ruby	B-Language
enum.inject	O
( initval	O
,	O
&	O
block	O
)	O
enum.reduce	O
( initval	O
,	O
&	O
block	O
)	O
enum.reverse_each.inject	O
( initval	O
,	O
&	O
block	O
)	O
enum.reverse_each.reduce	O
( initval	O
,	O
&	O
block	O
)	O
enum.inject	O
( &block	O
)	O
enum.reduce	O
( &block	O
)	O
enum.reverse_each.inject	O
( &block	O
)	O
enum.reverse_each.reduce	O
( &block	O
)	O
In	O
Ruby	B-Language
1.8.7	O
+	O
,	O
can	O
also	O
pass	O
a	O
symbol	O
representing	O
a	O
function	O
instead	O
of	O
a	O
block	O
.	O
</s>
<s>
enum	O
is	O
an	O
Enumeration	O
Please	O
notice	O
that	O
these	O
implementations	O
of	O
right	B-Application
folds	I-Application
are	O
wrong	O
for	O
non-commutative	O
&	O
block	O
(	O
also	O
initial	O
value	O
is	O
put	O
on	O
wrong	O
side	O
)	O
.	O
</s>
<s>
Rust	B-Application
iterator.fold(initval, func )	O
iterator.rev( )	O
.fold(initval, func )	O
iterator.rev( )	O
requires	O
iterator	O
to	O
be	O
a	O
DoubleEndedIterator	O
.	O
</s>
<s>
Scala	B-Application
list.foldLeft(initval )	O
(	O
func	O
)	O
(	O
initval	O
/	O
:	O
list	O
)	O
(	O
func	O
)	O
list.foldRight(initval )	O
(	O
func	O
)	O
(	O
list	O
:\	O
initval	O
)	O
(	O
func	O
)	O
list.reduceLeft(func )	O
list.reduceRight(func )	O
Scala	B-Application
's	O
symbolic	O
fold	O
syntax	O
was	O
intended	O
to	O
resemble	O
the	O
left	O
-	O
or	O
right-leaning	O
tree	O
commonly	O
used	O
to	O
explain	O
the	O
fold	O
operation	O
,	O
but	O
has	O
since	O
been	O
reinterpreted	O
as	O
an	O
illustration	O
of	O
a	O
toppling	O
domino	O
.	O
</s>
<s>
The	O
colon	O
comes	O
from	O
a	O
general	O
Scala	B-Application
syntax	O
mechanism	O
whereby	O
the	O
apparent	O
infix	O
operator	O
is	O
invoked	O
as	O
a	O
method	O
on	O
the	O
left	O
operand	O
with	O
the	O
right	O
operand	O
passed	O
as	O
an	O
argument	O
,	O
or	O
vice	O
versa	O
if	O
the	O
operator	O
's	O
last	O
character	O
is	O
a	O
colon	O
,	O
here	O
applied	O
symmetrically	O
.	O
</s>
<s>
Scala	B-Application
also	O
features	O
the	O
tree-like	O
folds	O
using	O
the	O
method	O
list.fold(z )	O
(	O
op	O
)	O
.	O
</s>
<s>
Scheme	B-Language
R6RS	O
(	O
fold-left	O
func	O
initval	O
list	O
)	O
(	O
vector-fold	O
func	O
initval	O
vector	O
)	O
(	O
fold-right	O
func	O
initval	O
list	O
)	O
(	O
vector-fold-right	O
func	O
initval	O
vector	O
)	O
(	O
reduce-left	O
func	O
defaultval	O
list	O
)	O
(	O
reduce-right	O
func	O
defaultval	O
list	O
)	O
(	O
unfold	B-Application
p	O
f	O
g	O
seed	O
 [ tail-gen ] 	O
)	O
unfold-right	O
p	O
f	O
g	O
seed	O
[	O
tail]( 	O
vector-unfold	O
f	O
length	O
initial-seed	O
···	O
)	O
(	O
vector-unfold-right	O
f	O
length	O
initial-seed	O
···	O
)	O
srfi/1	O
srfi/43	O
Smalltalk	B-Application
aCollection	O
inject	O
:	O
aValue	O
into	O
:	O
aBlock	O
aCollection	O
reduce	O
:	O
aBlock	O
ANSI	O
Smalltalk	B-Application
does	O
n't	O
define	O
#reduce	O
:	O
but	O
many	O
implementations	O
do	O
.	O
</s>
<s>
Standard	B-Language
ML	I-Language
foldl	B-Application
func	O
initval	O
list	O
Array.foldl	O
func	O
initval	O
array	O
foldr	B-Application
func	O
initval	O
list	O
Array.foldr	O
func	O
initval	O
array	O
The	O
supplied	O
function	O
takes	O
its	O
arguments	O
in	O
a	O
tuple	O
.	O
</s>
<s>
For	O
foldl	B-Application
,	O
the	O
folding	O
function	O
takes	O
arguments	O
in	O
the	O
same	O
order	O
as	O
for	O
foldr	B-Application
.	O
</s>
<s>
Fold	O
is	O
a	O
polymorphic	B-Application
function	I-Application
.	O
</s>
<s>
Also	O
,	O
in	O
a	O
lazy	B-Language
language	I-Language
with	O
infinite	O
lists	O
,	O
a	O
fixed	B-Application
point	I-Application
combinator	I-Application
can	O
be	O
implemented	O
via	O
fold	O
,	O
proving	O
that	O
iterations	O
can	O
be	O
reduced	O
to	O
folds	O
:	O
</s>
