Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
Last active December 15, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlhaufe/5263114 to your computer and use it in GitHub Desktop.
Save mlhaufe/5263114 to your computer and use it in GitHub Desktop.
Using fake operator overloading to simulate function composition
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