Overview
Implement compose(...fns) that returns a function applying functions from right to left.
Constraints
- Each function takes and returns one value.
- No functions = identity.
Examples
compose(x => x + 1, x => x * 2)(3); // 7
Solution
Reveal solution
function compose(...fns) {
return function(x) {
return fns.reduceRight((acc, fn) => fn(acc), x);
};
}compose.js
Compose
easycodingJavaScriptFunctions
Overview
Implement compose(...fns) that returns a function applying functions from right to left.
Constraints
- Each function takes and returns one value.
- No functions = identity.
Examples
compose(x => x + 1, x => x * 2)(3); // 7
Solution
Reveal solution
function compose(...fns) {
return function(x) {
return fns.reduceRight((acc, fn) => fn(acc), x);
};
}NameTopicDifficulty
103 of 103 problems