1 karma · joined Aug 2, 2012 (14y ago) · 1 submissions
comment 14y ago · link · parent
https://github.com/austinyun/challenges/blob/master/array-it...
function increment_where_pure(array, match, n) {
var count;
function iterator(f, acc, item) {
// f is the function used to combine array arguments -- push || unshift
if (count === 0) {
f.call(acc, item);
} else {
if (item === match) { item += 1; }
f.call(acc, item);
count -= 1;
}
return acc;
}
if (n === 0) {
return _.map(array, function(item) {
return item === match ? item + 1 : item;
});
}
if (n > 0) {
count = n + 1;
return _.reduce(array, iterator.bind(null, Array.prototype.push), []);
}
if (n < 0) {
count = -n + 1;
return _.reduceRight(array, iterator.bind(null, Array.prototype.unshift), []);
}
}