1% Generated by roxygen2: do not edit by hand
2% Please edit documentation in R/fold.R
3\name{fold}
4\alias{fold}
5\title{Efficient Fold, Reduce, Accumulate, Combine of a Vector}
6\usage{
7fold(x, f, left = TRUE, unname = TRUE, threshold = 1000L)
8}
9\arguments{
10\item{x}{A vector.}
11
12\item{f}{A binary function, i.e. a function take takes two arguments.}
13
14\item{left}{If \code{TRUE}, vector is combined from the left (the first element),
15otherwise the right (the last element).}
16
17\item{unname}{If \code{TRUE}, function \code{f} is called as
18\code{f(unname(y), x[[ii]])}, otherwise as \code{f(y, x[[ii]])},
19which may introduce name \code{"y"}.}
20
21\item{threshold}{An integer (>= 2) specifying the length where the
22recursive divide-and-conquer call will stop and incremental building of
23the partial value is performed.  Using \code{threshold = +Inf} will disable
24recursive folding.}
25}
26\value{
27A vector.
28}
29\description{
30Efficient Fold, Reduce, Accumulate, Combine of a Vector
31}
32\details{
33In order for recursive folding to give the same results as non-recursive
34folding, binary function \code{f} must be \emph{associative} with itself, i.e.
35\code{f(f(x[[1]], x[[2]]), x[[3]])} equals
36\code{f(x[[1]], f(x[[2]]), x[[3]])}.
37
38This function is a more efficient (memory and speed) of
39\code{\link[base:funprog]{Reduce(f, x, right = !left, accumulate = FALSE)}},
40especially when \code{x} is long.
41}
42\keyword{internal}
43