Catamorphism

From Rosetta Code
Catamorphism is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Reduce is a function or method that is used to take the values in an array or a list and reduce them to one value depending on a specified function. See also Fold.


JavaScript

<lang javascript>var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function add(a, b) {

   return a + b;

}

var summation = nums.reduce(add);

function mul(a, b) {

   return a * b;

}

var product = nums.reduce(mul, 1);

var concatenation = nums.reduce(add, "");

console.log(summation, product, concatenation); </lang>