Write a `compose(...fns)` that composes functions right-to-left. `compose(f, g)(x)` should equal `f(g(x))`.
Problem Statement
Write a `compose(...fns)` that composes functions right-to-left. `compose(f, g)(x)` should equal `f(g(x))`.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Function composition applies functions in sequence.
š” Hint 2:
Use reduceRight to apply functions from right to left.
š” Hint 3:
return (x) => fns.reduceRight((acc, fn) => fn(acc), x);
ā
Solution:
```javascript
function compose(...fns) {
return (x) => fns.reduceRight((acc, fn) => fn(acc), x);
}
```