Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Created September 6, 2012 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ympbyc/3654115 to your computer and use it in GitHub Desktop.
Save ympbyc/3654115 to your computer and use it in GitHub Desktop.
fuck prototype
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