Last active
December 15, 2015 12:49
-
-
Save mlhaufe/5263114 to your computer and use it in GitHub Desktop.
Using fake operator overloading to simulate function composition
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
Function.prototype.valueOf = function(){ | |
id.fs.push(this) | |
} | |
function id(){ | |
var fs = [] | |
function f(x){ | |
for(var i=0,len=fs.length,v=x;i<len;v=fs[i++](v)); | |
return v | |
} | |
f.valueOf = function(){ id.fs = fs } | |
return f | |
} | |
function sq(x){ return x + x } | |
function neg(x){ return -x } | |
function mult2(x){ return x * 2 } | |
function add1(x){ return x + 1 } | |
var f = id(); | |
f >> sq >> neg >> mult2 >> add1; | |
f(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment