The OpenD Programming Language

fold.fold

Implements the homonym function (also known as accumulate, compress, inject, or foldl) present in various programming languages of functional flavor.

fold is functionally equivalent to reduce except the range parameter comes first and there is no need to use `tuple` for multiple seeds.

There may be one or more callable entities (functions argument) to apply.

template fold(functions...)
fold
(
Args...
)
(
Args args
)

Parameters

args Args

Just the range to fold over; or the range and one seed per function; or the range, one seed per function, and the work unit size

Return Value

Type: auto

The accumulated result as a single value for single function and as a tuple of values for multiple functions

Examples

static int adder(int a, int b)
{
    return a + b;
}
static int multiplier(int a, int b)
{
    return a * b;
}

// Just the range
auto x = taskPool.fold!adder([1, 2, 3, 4]);
assert(x == 10);

// The range and the seeds (0 and 1 below; also note multiple
// functions in this example)
auto y = taskPool.fold!(adder, multiplier)([1, 2, 3, 4], 0, 1);
assert(y[0] == 10);
assert(y[1] == 24);

// The range, the seed (0), and the work unit size (20)
auto z = taskPool.fold!adder([1, 2, 3, 4], 0, 20);
assert(z == 10);

See Also

Similar to std.algorithm.iteration._fold, fold is a wrapper around reduce.

Meta