Created
October 20, 2013 10:35
-
-
Save ttanimichi/7067825 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
window.print = function(s) { | |
document.write(s); | |
}; | |
window.puts = function(s) { | |
document.write(s); | |
document.write("<br>"); | |
}; | |
Array.prototype.equals = function(other) { | |
for(var i=0;i<this.length;i++) { | |
if(this[i] != other[i]) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
Array.prototype.toString = function() { | |
var str = "["; | |
for(var i=0; i < this.length; i++) { | |
str += this[i].toString(); | |
if(i != (this.length-1)) { | |
str += ", "; | |
} | |
} | |
return str+"]"; | |
}; | |
Array.prototype.clone = function() { | |
var cloned = []; | |
for(var i=0;i<this.length;i++) { | |
cloned.push(this[i]) | |
} | |
return cloned; | |
}; | |
Array.prototype.each = function(fn) { | |
for(var i=0; i < this.length; i++) { | |
fn(this[i]); | |
} | |
}; | |
Array.prototype.map = function(fn) { | |
var mapped = []; | |
for(var i=0; i < this.length; i++) { | |
mapped.push(fn(this[i])); | |
} | |
return mapped; | |
}; | |
Array.prototype.reduce = function(fn) { | |
var ary = this.clone(); | |
while(true) { | |
if(ary.length == 0) { | |
return null; | |
} | |
if(ary.length == 1) { | |
return ary[0]; | |
} | |
var elem1 = ary.shift(); | |
var elem2 = ary.shift(); | |
ary.unshift(fn(elem1,elem2)); | |
} | |
}; | |
Array.prototype.select = function(fn) { | |
var selected = []; | |
for(var i=0; i < this.length; i++) { | |
if(fn(this[i])) { | |
selected.push(this[i]); | |
} | |
} | |
return selected; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment