Functional programming


Example

const add = (a, b) => a + b;
const sum = arr => arr.reduce(add, 0);
const average = arr => sum(arr) / arr.length;

average([5, 10, 15, 20]);
12.5
const pow = exponent => base =>
  Math.pow(base, exponent);
const square = pow(2);

pow(2);
base => Math.pow(base, exponent)
pow(2)(8);
64
const square = pow(2);
square(5);
25
const sumOfSquares = arr => {
  const avg = average(arr);
  return sum(arr
    .map(val => val - avg)
    .map(square));
};

sumOfSquares([5, 10, 15, 20]);
125

External Resources