Last active
March 27, 2017 16:22
-
-
Save buzzdecafe/b00905f6487725173ff7 to your computer and use it in GitHub Desktop.
curried binary compose
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
// 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