I

reduce

@ryanwgough

"Apply a function against an accumulator and each member of an array"

var someNumbers = [1, 2, 3, 4, 5];

var sum = someNumbers.reduce(
    (acc, item) => acc + item,
    0
);

// sum === 15

Sum an array

var someColours = [
    'red', 'blue', 'green', 'red', 'blue', 'orange'
];


var count = someColours.reduce(function(acc, item) {
    acc[item] = acc[item] ? acc[item] + 1 : 1;
    return acc;
}, {});

// count === { red: 2, blue: 2, green: 1, orange: 1 }

Produce a count

function addTwo(item) {
    return item + 2;
}

function negate(item) {
    return 0 - item;
}

var functions = [addTwo, negate];

var answer = functions.reduce(function(acc, item) {
    return item(acc);
}, 2);

// answer === -4

ChAIN Functions

And now for some chin stroking...

Abstraction

Simplicity

Recursion

I

reduce

@ryanwgough

deck

By Ryan Gough

deck

  • 318