Last active
July 8, 2016 12:22
-
-
Save khaled0fares/f8b2179f155b0b76997ce44e0ae3f740 to your computer and use it in GitHub Desktop.
Simple implementation for some Enumerable functions in JS
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.transform = function(fn){ //map | |
let newList = [], i = 0, len = this.length; | |
for(i; i < len; i++){ | |
newList.push(fn(this[i])); | |
} | |
return newList; | |
} | |
Array.prototype.exclude = function(fn){ //filter | |
let filteredList = [], i = 0, len = this.length; | |
for(i; i < len; i++){ | |
if(fn(this[i])){ | |
filteredList.push(this[i]); | |
}; | |
} | |
return filteredList | |
} | |
Array.prototype.aggregate = function(fn, init){ //reduce | |
let total = init,i = 0, len = this.length; | |
for(i; i < len; i++){ | |
total = fn(total,this[i]) | |
} | |
return total | |
} | |
Array.prototype.iterate = function(fn){ //forEach | |
let i = 0, len = this.length; | |
for(i; i < len; i++){ | |
fn(this[i]); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment