Skip to content

Commit

Permalink
Merge pull request #60 from stoeffel/compose-pass-all-args
Browse files Browse the repository at this point in the history
pass all arguments to the first function
  • Loading branch information
tjmehta committed Feb 15, 2015
2 parents 304649c + c516cdf commit ac78e89
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
13 changes: 13 additions & 0 deletions test/test-compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down

0 comments on commit ac78e89

Please sign in to comment.