diff --git a/compose.js b/compose.js index 99b9045..553be6f 100644 --- a/compose.js +++ b/compose.js @@ -12,7 +12,9 @@ module.exports = compose; function compose(f,g) { - return function composed(x) { - return f(g(x)); + return function composed(/* args */) { + var args = Array.prototype.slice.call(arguments); + + return f(g.apply(null, args)); } } diff --git a/test/test-compose.js b/test/test-compose.js index 80ec0de..0d0020d 100644 --- a/test/test-compose.js +++ b/test/test-compose.js @@ -17,6 +17,19 @@ describe('compose', function() { done(); }); + it('compose(f, sum)(x, y) should be identical to f(sum(x, y))', function(done) { + var f = function(x) { return x * x; } + var sum = function(x, y) { return x + y; } + var x = Math.random(); + var y = Math.random(); + var expected = f(sum(x, y)); + var composed = compose(f, sum); + var actual = composed(x, y); + expect(actual).to.eql(expected); + done(); + }); + + it('compose(f, g)(x) should be identical to [x].map(g).map(f)[0]', function(done) { var f = function(x) { return -x; } var g = function(x) { return x * 5; }