Created
September 6, 2012 09:54
-
-
Save ympbyc/3654115 to your computer and use it in GitHub Desktop.
fuck prototype
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
Boolean.prototype.ifTrue = function ( exp ) { if (this.valueOf()) return exp() ; return null ;}; | |
Boolean.prototype.ifFalse = function ( exp ) { if ( ! this.valueOf()) return exp() ; return null ;}; | |
Boolean.prototype.ifTrue_ifFalse = function (t,f) { if (this.valueOf()) return t(); return f(); }; | |
Boolean.prototype.and = function (fn) { return this.ifTrue_ifFalse(fn, function () {return false}) } | |
Boolean.prototype.or = function (fn) { return this.ifTrue_ifFalse(function () {return true}, function () {fn().ifTrue_ifFalse(function () {return true;}, function () {return false;})} ) }; | |
Boolean.prototype.not = function () { return ! this }; | |
Object.prototype.at = function (index) { return this[index] ;}; | |
Object.prototype.at_put = function (index, value) { this[index] = value; return this ;}; | |
Object.prototype.do = function (fn) { for (var key in this) if (this.hasOwnProperty(key)) fn(this[key], key); return null }; | |
Object.prototype.detect = function (fn) { var res = null; this.do(function (it, key) { if (res) return ; if (fn(it)) res = it;}); return res }; | |
Object.prototype.select = function (fn) { var res = Object.new_(); this.do(function (it, key) { if (fn(it)) res[key] = it; }); return res}; | |
Object.prototype.reject = function (fn) { var res = Object.new_(); this.do(function (it, key) { if ( ! fn(it)) res[key] = it; }); return res}; | |
Object.prototype.collect = function (fn) { var res = Object.new_(); this.do(function (it, key) { res[key] = fn(it); }); return res; }; | |
Object.prototype.inject_into = function (init, fn) { var lastres = init; this.do(function (it) { lastres = fn(it, lastres) }); return lastres; }; | |
Object.prototype.eq = function (obj) { return this === obj; }; | |
Object.prototype.notEq = function (obj) { return this !== obj; }; | |
Object.prototype.copy = function () { return this }; | |
Object.prototype.deepCopy = function () { var res = this.constructor.new_(); this.do(function (it, key) { res[key] = it; }); return res; }; | |
Object.prototype.respondsTo = function (messageString) { return Boolean(this[messageString]) }; | |
Object.prototype.between_and = function (a, b) { var res = true; this.do(function (it) { (it >= a).and(function () {return it <= b}).ifFalse(function () {res = false}) }); return res }; | |
Object.prototype.toArray = function () { var res = Array.new_(); this.do(function (it) {res.addLast(it)} ); return res; }; | |
Object.prototype.max = function () { return Math.max.apply(this, this.toArray()); }; | |
Object.prototype.max = function () { return Math.min.apply(this, this.toArray()); }; | |
Function.prototype.new_ = function () { var o = new this; this.apply(o, arguments); return o; }; | |
Array.prototype.select = function (fn) { var res = []; this.do(function (it) { if (fn(it)) res.push(it); }); return res}; | |
Array.prototype.reject = function (fn) { var res = []; this.do(function (it) { if ( ! fn(it)) res.addLast(it); }); return res}; | |
Array.prototype.collect = function (fn) { var res = []; this.do(function (it) { res.addLast(fn(it)); }); return res; }; | |
Array.prototype.addLast = function (val) { this.push(val); return this; }; | |
Number.prototype.to = function (tonum) { var i = this-1, res = Array.new_(); while (++i < tonum) res.addLast(i); return res;}; | |
Number.prototype.timesRepeat = function (fn) { return (0).to(this).do(function (it) {fn(it)}); }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment