Skip to content

Instantly share code, notes, and snippets.

@khaled0fares
Last active July 8, 2016 12:22
Show Gist options
  • Save khaled0fares/f8b2179f155b0b76997ce44e0ae3f740 to your computer and use it in GitHub Desktop.
Save khaled0fares/f8b2179f155b0b76997ce44e0ae3f740 to your computer and use it in GitHub Desktop.
Simple implementation for some Enumerable functions in JS
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