Last active
February 11, 2022 17:44
-
-
Save griimick/45c59b6cd85e6a31e0417ab92abe96af to your computer and use it in GitHub Desktop.
Map Reduce Filter
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.protoype.filter | |
The filter() method creates a new array with all elements that pass the test implemented by the callback. | |
Syntax: | |
* callback function should return a boolean (true or false) | |
* callback function can take three paramters, namely cuurrentValue, index, and array. | |
* thisArg can be passed if the callback function is required to be called with a different this. | |
Signatures: | |
filter(callbackFn) | |
filter(callbackFn, thisArg) | |
filter(function callbackFn(currentValue) { ... }) | |
filter(function callbackFn(currentValue, index) { ... }) | |
filter(function callbackFn(currentValue, index, array){ ... }) | |
filter(function callbackFn(currentValue, index, array) { ... }, thisArg) | |
*/ | |
// Custom Polyfill | |
Array.prototype.filter = function(callback, thisArg) { | |
var result = []; | |
for (var i = 0; i < this.length; ++i) { | |
if (callback.call(thisArg, this[i], i, this)) | |
result.push(this[i]); | |
} | |
return result; | |
} | |
// Proper Polyfill with error handling | |
Array.prototype.betterFilter = function(callback /*, thisArg */) { | |
if (this == null) | |
throw new TypeError('betterFilter called on null or not defined'); | |
if (typeof function !== 'function') | |
throw new TypeError('callback is not a function'); | |
var thisArg, counter = 0; | |
var len = this.length; | |
var res = new Array(len); | |
if (arguments.length >= 2) | |
thisArg = arguments[1]; | |
while(counter < len) { | |
if (callback.call(thisArg, this[counter], counter, this)) | |
res[counter] = this[counter]; | |
} | |
return res; | |
} |
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.map() | |
map() method creates a new array populated with the results of calling a provided function on every element in the array. | |
Syntax: | |
* thisArg is the value of this the callback function will use when being called with map | |
* the callback function takes in three elements, namely currentElement, index, and array. | |
Sample signatues: | |
map(vallbackFn, thisArg); | |
map(callbackFn); | |
map(function callbackFn(currentElement) {....}) | |
map(function callbackFn(currentElement, index) {....}) | |
map(function callbackFn(currentElement, index, array) {....}) | |
map(function callbackFn(currentElement, index, array) {....}, thisArg) | |
*/ | |
// Custom polyfil implementation | |
Array.prototype.customMap = function (callback) { | |
var arr = []; | |
for (int i = 0; i < this.length; ++i) { | |
arr.push(callback(this[i], i, this)); | |
} | |
return arr; | |
} | |
// Custom polyfill with error scenario handling | |
Array.prototype.betterMap = functin (callback, /* thisArg */) { | |
if (this == null) | |
throw new TypeError("this is null or not defined"); | |
if (typeof callback !== 'function') | |
throw new TypeError("callback is not a function"); | |
var thisArg, newArr, counter; | |
var len = this.length; | |
if (arguments.length > 1) | |
thisArg = arguments[1]; | |
newArr = new Array(len); | |
counter = 0; | |
while(counter < len) { | |
newArr[counter] = callback.call(thisArg, this[counter], counter, this); | |
counter++; | |
} | |
return newArr; | |
} |
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.reduce() | |
The reduce() method executes a reducer function (callback) on each element of the array, resulting in a single output value. | |
Syntax: | |
* the reducer function takes in four four arguments, namely Accumulator, curren value, current index, & source array. | |
* in absence of an initial value, the first value of the array is used as the starting point for reduce function | |
Sample signatures: | |
reduce(reducerFn) | |
reduce(reducerFn, initialValue) | |
reduce(function reducerFn(accumulator, currentValue) { ... }) | |
reduce(function reducerFn(accumulator, currentValue, index) { ... }) | |
reduce(function reducerFn(accumulator, currentValue, index, array){ ... }) | |
reduce(function reducerFn(accumulator, currentValue, index, array) { ... }, initialValue) | |
*/ | |
// Custom Polyfill | |
Array.prototype.customReduce = function(callback, initialValue) { | |
var accumulator = intialValue === undefined ? undefined : intialValue; | |
for (var i = 0; i < this.length; i++) { | |
if (accumulator !== undefined) | |
accumulator = callback.call(undefined, accumulator, this[i], i, this); | |
else | |
accumulator = this[i]; | |
} | |
return accumulator; | |
} | |
// Proper Polyfill with error handling | |
Array.prototype.betterReduce = function(callback /*, intialVlaue */) { | |
if (this == null) | |
throw new TypeError('betterReduce called on null or not defined'); | |
if (typeof callback !== 'function') | |
throw new TypeError('callback is not a function '); | |
var Obj = this; | |
var len = this.length; | |
var counter = 0; | |
var value; | |
if (arguments.length >= 2) | |
value = arguments[1]; | |
while (counter < len) { | |
value = callback(value, this[counter], counter, this); | |
} | |
return value; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment