Last active
January 5, 2016 15:53
-
-
Save Quaggie/962a0e140dd1d8d95f0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.concatAll = function() { | |
var results = []; | |
this.forEach(function(subArray) { | |
results.push.apply(results, subArray); | |
}); | |
return results; | |
}; | |
Array.prototype.concatMap = function(fn) { | |
return this. | |
map(function(item) { | |
return fn(item); | |
}).concatAll(); | |
}; | |
Array.prototype.reduce = function(combiner, initialValue) { | |
var counter, | |
accumulatedValue; | |
if (this.length === 0) { | |
return this; | |
} | |
else { | |
if (arguments.length === 1) { | |
counter = 1; | |
accumulatedValue = this[0]; | |
} | |
else if (arguments.length >= 2) { | |
counter = 0; | |
accumulatedValue = initialValue; | |
} | |
else { | |
throw "Invalid arguments."; | |
} | |
while(counter < this.length) { | |
accumulatedValue = combiner(accumulatedValue, this[counter]); | |
counter++; | |
} | |
return [accumulatedValue]; | |
} | |
}; | |
Array.prototype.zip = function(left, right, combinerFunction) { | |
var counter, | |
results = []; | |
for(counter = 0; counter < Math.min(left.length, right.length); counter++) { | |
results.push(combinerFunction(left[counter],right[counter])); | |
} | |
return results; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment