Functional application: Allow any granularity of arguments (currying, essentially).

This commit is contained in:
David Braun 2017-10-17 10:34:17 -04:00
parent 04bbe30a00
commit d7c117619a
3 changed files with 17 additions and 31 deletions

View file

@ -339,23 +339,22 @@ Channel.select = methodPromises =>
// functional interface allowing full or partial application
//
// Channel.slice(10, Infinity, channel)
//
// or
//
// const skipTen = Channel.slice(10, Infinity)
// skipTen(channel)
// Channel.slice(10, Infinity)(channel)
// Channel.slice(10)(Infinity)(channel)
const channel = Channel();
const methods = Object.keys(channel).concat(Object.keys(channel.readOnly()));
methods.forEach(method => {
Channel[method] = (...args) => {
const bound = function(...args) {
const arity = method === `slice` ? 3 : channel[method].length;
return args.length >= arity
? args[arity - 1][method](...args.slice(0, arity - 1))
: channel => channel[method](...args);
: bound.bind(this, ...args);
};
Channel[method] = bound;
});
module.exports = Object.freeze(Channel);