Skip to content

Instantly share code, notes, and snippets.

@guilhermepontes
Last active December 5, 2016 11:47
Show Gist options
  • Save guilhermepontes/555ff547a2d6a4689ed51aba08095463 to your computer and use it in GitHub Desktop.
Save guilhermepontes/555ff547a2d6a4689ed51aba08095463 to your computer and use it in GitHub Desktop.
Reduce
var votes = [
'angular',
'angular',
'react',
'react',
'react',
'angular',
'ember',
'react',
'vanilla'
];
var initialValue = {};
var reducer = function(tally, vote) {
if(!tally[vote]) {
tally[vote] = 1;
} else {
tally[vote] = tally[vote] + 1;
}
return tally;
}
var result = votes.reduce(reducer, initialValue);
console.log(result);
// Object {angular: 3, react: 4, ember: 1, vanilla: 1}
var data = [1, 2, 3];
var doubled = data.reduce(function(acc, value) {
acc.push(value * 2);
return acc;
}, []);
var doubleMapped = data.map(function(item) {
return item * 2;
});
var data2 = [1, 2, 3, 4, 5, 6];
var evens = data2.reduce(function(acc, value) {
if (value % 2 === 0) {
acc.push(value);
}
return acc;
}, []);
var evenFiltered = data2.filter(function(item) {
return (item % 2 === 0);
});
var filterMapped = data2.filter(function(value) {
return value % 2 === 0;
}).map(function(value) {
return value * 2;
});
var bigData = [];
for (var i = 0; i < 1000000; i++) {
bigData[i] = i;
}
// console.time('bigData');
var filterMappedBigData = bigData.filter(function(value) {
return value % 2 === 0;
}).map(function(value) {
return value * 2;
});
// console.timeEnd('bigData');
// console.time('bigDataReduce');
var reducedBigData = bigData.reduce(function(acc, value) {
if (value % 2 === 0) {
acc.push(value * 2);
}
return acc;
}, []);
// console.timeEnd('bigDataReduce');
function reducer(accumulator, value, index, array) {
var intermediaryValue = accumulator + value;
if (index === array.length - 1) {
return intermediaryValue / array.length;
}
return intermediaryValue;
}
var data = [1, 2, 3, 3, 4, 5, 3, 1];
var mean = data.reduce(reducer, 0);
console.log(mean);
var data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
var flattenedData = data.reduce(function(acc, value) {
return acc.concat(value);
}, []);
var input = [
{
title: "Batman Begins",
year: 2005,
cast: [
"Christian Bale",
"Michael Caine",
"Liam Neeson",
"Katie Holmes",
"Gary Oldman",
"Cillian Murphy"
]
},
{
title: "The Dark Knight",
year: 2008,
cast: [
"Christian Bale",
"Heath Ledger",
"Aaron Eckhart",
"Michael Caine",
"Maggie Gyllenhal",
"Gary Oldman",
"Morgan Freeman"
]
},
{
title: "The Dark Knight Rises",
year: 2012,
cast: [
"Christian Bale",
"Gary Oldman",
"Tom Hardy",
"Joseph Gordon-Levitt",
"Anne Hathaway",
"Marion Cotillard",
"Morgan Freeman",
"Michael Caine"
]
}
];
var stars = input.reduce(function(acc, value) {
value.cast.forEach(function(star) {
if (acc.indexOf(star) === -1) {
acc.push(star);
}
});
return acc;
}, []);
var data = [1, 2, 3, 4, "5"];
var sum = data.reduceRight(function(acc, value, index) {
console.log(index);
return acc + value;
}, 0);
console.log(sum);
function increment(input) { return input + 1;}
function decrement(input) { return input - 1; }
function double(input) { return input * 2; }
function halve(input) { return input / 2; }
var initial_value = 1;
var pipeline = [
increment,
increment,
increment,
double,
increment,
increment,
halve
];
var final_value = pipeline.reduce(function(acc, fn) {
return fn(acc);
}, initial_value);
var reversed = pipeline.reduceRight(function(acc, fn) {
return fn(acc);
}, initial_value)
console.log(final_value, reversed);
var luke = {
name: "luke skywalker",
jedi: true,
parents: {
father: {
jedi: true
},
mother: {
jedi: false
}
}
}
var han = {
name: "han solo",
jedi: false,
parents: {
father: {
jedi: false
},
mother: {
jedi: false
}
}
}
var anakin = {
name: "anakin skywalker",
jedi: true,
parents: {
mother: {
jedi: false
}
}
}
var characters = [luke, han, anakin];
function fatherWasJedi(character) {
var path = "parents.father.jedi";
return path.split(".").reduce(function(obj, field) {
if (obj) {
return obj[field];
}
return false;
}, character);
}
characters.forEach(function(character) {
console.log(character.name + "'s father was a jedi:", fatherWasJedi(character));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment