Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value.



Task

Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language.



See also

 Wikipedia article:   Fold
 Wikipedia article:   Catamorphism


void main() {
    import std.stdio, std.algorithm, std.range, std.meta, std.numeric,
           std.conv, std.typecons;
 
    auto list = iota(1, 11);
    alias ops = AliasSeq!(q{a + b}, q{a * b}, min, max, gcd);
 
    foreach (op; ops)
        writeln(op.stringof, ": ", list.reduce!op);
 
    // std.algorithm.reduce supports multiple functions in parallel:
    reduce!(ops[0], ops[3], text)(tuple(0, 0.0, ""), list).writeln;
}