-
-
Save bttmly/9549a6392bd6731afa02 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
var compose = function compose(f) { | |
var queue = f ? [f] : []; | |
var fn = function fn(g) { | |
if (arguments.length) { | |
queue.push(g); | |
return fn; | |
} | |
return function() { | |
var args = Array.prototype.slice.call(arguments); | |
queue.forEach(function(func) { | |
args = [func.apply(this, args)]; | |
}); | |
return args[0]; | |
} | |
}; | |
return fn; | |
}; |
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
//------------------------------------------------------------------------------ | |
var add1 = function(x) {return x + 1;}; | |
var mult2 = function(x) {return x * 2;}; | |
var square = function(x) {return x * x;}; | |
var negate = function(x) {return -x;}; | |
//------------------------------------------------------------------------------ | |
var f = compose(add1)(mult2)(square)(negate)(); | |
console.assert(-36, f(2), "f(2)"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment