View source code
Display the source code in std/parallelism.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.parallelism.TaskPool.fold.fold

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

auto auto fold(Args...) (
  Args args
);

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.

Parameters

NameDescription
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

Returns

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

See Also

Similar to fold, fold is a wrapper around reduce.

Example

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);

Authors

License

Boost License 1.0