Exercise 7: Compose Function (right to left)
Problem Statement
Create a function `compose(...fns)` that composes functions (right to left). Example: compose(f, g, h)(x) → f(g(h(x)))
Sample Output:
compose((x) => x * 2, (x) => x + 1)(5) => 12 // Steps: 5+1=6, 6*2=12
Solution
const compose = (...fns) => (x) => fns.reduceRight((acc, fn) => fn(acc), x);Explanation
Overall Goal:
- Functions ko right-to-left compose karna.
Solution:
reduceRightse right se left process.
Real world:
- Functional programming: function composition.
- Data pipelines: transformation chains.