Created
December 15, 2014 15:40
-
-
Save dholdren/67f5927c91bb4d09aed0 to your computer and use it in GitHub Desktop.
map implementations in javascript
This file contains hidden or 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 = function(f) { | |
res = new Array(this.length); | |
for (i=0;i < this.length; i++) { | |
res[i] = f(this[i]); | |
} | |
return res; | |
}; | |
new_a = [1,2,3,4].map(function(ele){ return ele*2; }); | |
console.log("new_a: "+new_a); | |
Array.prototype.map_in_place = function(f) { | |
for (i=0;i < this.length; i++) { | |
this[i] = f(this[i]); | |
} | |
return this; | |
}; | |
a = [1,2,3,4]; | |
console.log("a before map_in_place: "+a); | |
a.map_in_place(function(ele){ return ele*5; }); | |
a; | |
console.log("a after map_in_place: "+a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment