Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Last active March 27, 2017 16:22
Show Gist options
  • Save buzzdecafe/b00905f6487725173ff7 to your computer and use it in GitHub Desktop.
Save buzzdecafe/b00905f6487725173ff7 to your computer and use it in GitHub Desktop.
curried binary compose
// first try:
R.cc = R.curry(function cc(f, g) {
return R.curryN(g.length, function() {
var gargs = Array.prototype.slice.call(arguments, 0, g.length);
var fargs = Array.prototype.slice.call(arguments, g.length);
return f.apply(this, [g.apply(this, gargs)].concat(fargs));
});
});
// so this works:
R.cc(R.add, R.multiply)(2, 3, 1); //=> 7
// but this doesn't:
var am = R.cc(R.add, R.multiply);
R.cc(am, R.cc(R.add, R.multiply))(2, 3, 1, 4, 5); //=> function
// second try:
R.cc = R.curry(function cc(f, g) {
return R.curryN((g.length + f.length - 1), function() {
var gargs = Array.prototype.slice.call(arguments, 0, g.length);
var fargs = Array.prototype.slice.call(arguments, g.length, (g.length + f.length) - 1);
return f.apply(this, [g.apply(this, gargs)].concat(fargs));
});
});
// now this works:
R.cc(R.add, R.multiply)(2, 3, 1); //=> 7
// and so does this:
var am = R.cc(R.add, R.multiply);
R.cc(am, R.cc(R.add, R.multiply))(2, 3, 1, 4, 5); //=> 33
// 'cuz: ((2 * 3) + 1) * 4) + 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment