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


import system'collections;
import system'routines;
import extensions;
import extensions'text;
 
public program()
{
    var numbers := new Range(1,10).summarize(new ArrayList());
 
    var summary := numbers.accumulate(new Variable(0), (a,b => a + b));
 
    var product := numbers.accumulate(new Variable(1), (a,b => a * b));
 
    var concatenation := numbers.accumulate(new StringWriter(), (a,b => a.toPrintable() + b.toPrintable()));
 
    console.printLine(summary," ",product," ",concatenation)
}