reduce

Reduce an array of elements to a single element, using a user-supplied reductor function.

If the array is empty, return the default value for the element type.

If the array contains only one element, return that element.

Otherwise, the reductor function will be called on every member of the array and on every resulting element until there is only one element, which is then returned.

ReturnTypeOf!(ReduceFunc)
reduce
(
T
ReduceFunc
)
(
in T[] array
,
ReduceFunc func
)

Parameters

array T[]

the array to reduce

func ReduceFunc

the reductor function

Return Value

Type: ReturnTypeOf!(ReduceFunc)

the single element reduction

Examples

auto result = reduce([1, 17, 8, 12][], (int i, int j){ return i * j; });
test!("==")(result, 1632);

result = reduce("", (char c1, char c2) { return 'X'; });
test!("==")(result, char.init);

Meta