const pipe =
(...args) =>
params =>
args.reduce((pre, next) => next(pre), params);
const compose =
(...args) =>
params =>
args.reverse().reduce((pre, next) => next(pre), params);
const pipePromise = (...args) => {
const init = args.pop();
return (...arg) =>
args.reduce(
(sequence, func) => sequence.then(result => func.call(null, result)),
Promise.resolve(init.apply(null, arg)),
);
};
const composePromise = (...args) => {
const init = args.pop();
return (...arg) =>
args
.reverse()
.reduce(
(sequence, func) => sequence.then(result => func.call(null, result)),
Promise.resolve(init.apply(null, arg)),
);
};
export { pipe, compose, pipePromise, composePromise };
网友评论