Created
June 4, 2017 03:16
-
-
Save msalahz/acc757fb7776db10ab230cb4b78494f5 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
// Map accepts the projection function to be applied to each item in the source array, and returns the projected array. | |
Array.prototype.map = function (projectionFunction) { | |
var results = []; | |
this.forEach(function (itemInArray) { | |
// ------------ INSERT CODE HERE! ---------------------------- | |
// Apply the projectionFunction to each item in the array and add | |
// each result to the results array. | |
// Note: you can add items to an array with the push() method. | |
// ------------ INSERT CODE HERE! ---------------------------- | |
results.push(projectionFunction(itemInArray)); | |
}); | |
return results; | |
}; | |
// JSON.stringify([1,2,3].map(function(x) { return x + 1; })) === '[2,3,4]' | |
// The filter() function accepts a predicate. | |
// A predicate is a function that accepts an item in the array, and returns a boolean indicating | |
// whether the item should be retained in the new array. | |
Array.prototype.filter = function (predicateFunction) { | |
var results = []; | |
this.forEach(function (itemInArray) { | |
// ------------ INSERT CODE HERE! ---------------------------- | |
// Apply the predicateFunction to each item in the array. | |
// If the result is truthy, add the item to the results array. | |
// Note: remember you can add items to the array using the array's | |
// push() method. | |
// ------------ INSERT CODE HERE! ---------------------------- | |
if (predicateFunction(itemInArray)) { | |
results.push(itemInArray); | |
} | |
}); | |
return results; | |
}; | |
// JSON.stringify([1,2,3].filter(function(x) { return x > 2})) === "[3]" | |
//The concatAll() function iterates over each sub-array in the array and collects the results in a new, flat array. | |
// Notice that the concatAll() function expects each item in the array to be another array. | |
Array.prototype.concatAll = function () { | |
var results = []; | |
this.forEach(function (subArray) { | |
// ------------ INSERT CODE HERE! ---------------------------- | |
// Add all the items in each subArray to the results array. | |
// ------------ INSERT CODE HERE! ---------------------------- | |
results.push.apply(results, subArray); | |
}); | |
return results; | |
}; | |
// JSON.stringify([ [1,2,3], [4,5,6], [7,8,9] ].concatAll()) === "[1,2,3,4,5,6,7,8,9]" | |
// [1,2,3].concatAll(); // throws an error because this is a one-dimensional array | |
// Nearly every time we flatten a tree we chain map() and concatAll(). | |
// Sometimes, if we're dealing with a tree several levels deep, we'll repeat this combination many times in our code.To save on typing, | |
// let's create a concatMap function that's just a map operation, followed by a concatAll. | |
Array.prototype.concatMap = function (projectionFunctionThatReturnsArray) { | |
return this. | |
map(function (item) { | |
// ------------ INSERT CODE HERE! ---------------------------- | |
// Apply the projection function to each item. The projection | |
// function will return a new child array. This will create a | |
// two-dimensional array. | |
// ------------ INSERT CODE HERE! ---------------------------- | |
return projectionFunctionThatReturnsArray(item); | |
}). | |
// apply the concatAll function to flatten the two-dimensional array | |
concatAll(); | |
}; | |
/* | |
var spanishFrenchEnglishWords = [ ["cero","rien","zero"], ["uno","un","one"], ["dos","deux","two"] ]; | |
// collect all the words for each number, in every language, in a single, flat list | |
var allWords = [0,1,2]. | |
concatMap(function(index) { | |
return spanishFrenchEnglishWords[index]; | |
}); | |
return JSON.stringify(allWords) === '["cero","rien","zero","uno","un","one","dos","deux","two"]'; | |
*/ | |
// Let's add a reduce() function to the Array type. Like map. | |
// Take note this is different from the reduce in ES5, which returns a value instead of an Array! | |
// [1,2,3].reduce(function(accumulatedValue, currentValue) { return accumulatedValue + currentValue; }); === [6]; | |
// [1,2,3].reduce(function(accumulatedValue, currentValue) { return accumulatedValue + currentValue; }, 10); === [16]; | |
Array.prototype.reduce = function (combiner, initialValue) { | |
var counter, | |
accumulatedValue; | |
// If the array is empty, do nothing | |
if (this.length === 0) { | |
return this; | |
} else { | |
// If the user didn't pass an initial value, use the first item. | |
if (arguments.length === 1) { | |
counter = 1; | |
accumulatedValue = this[0]; | |
} else if (arguments.length >= 2) { | |
counter = 0; | |
accumulatedValue = initialValue; | |
} else { | |
throw "Invalid arguments."; | |
} | |
// Loop through the array, feeding the current value and the result of | |
// the previous computation back into the combiner function until | |
// we've exhausted the entire array and are left with only one function. | |
while (counter < this.length) { | |
accumulatedValue = combiner(accumulatedValue, this[counter]) | |
counter++; | |
} | |
return [accumulatedValue]; | |
} | |
}; | |
// Let's add a static zip() function to the Array type. | |
// The zip function accepts a combiner function, traverses each array at the same time, | |
// and calls the combiner function on the current item on the left- hand - side and right- hand - side. | |
// The zip function requires an item from each array in order to call the combiner function, | |
// therefore the array returned by zip will only be as large as the smallest input array. | |
// JSON.stringify(Array.zip([1,2,3],[4,5,6], function(left, right) { return left + right })) === '[5,7,9]' | |
Array.zip = function (left, right, combinerFunction) { | |
var counter, | |
results = []; | |
for (counter = 0; counter < Math.min(left.length, right.length); counter++) { | |
// Add code here to apply the combinerFunction to the left and right-hand items in the respective arrays | |
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