Build a new function by applying a list of functions in sequence to an input value.
Compose Functions
gfgFunction Composition
You are given an array of functions. Create and return a new function that, when called with one argument, passes that argument through the functions from right to left.
In other words, if the functions are , the composed function should behave like:
If the array of functions is empty, the returned function should behave like the identity function and return its input unchanged.
This problem focuses on understanding function chaining and evaluating a composed pipeline in the correct order.
Input Format
- An array of functions
functions, where each function takes one argument. - A single input value
xwhen the returned function is invoked.
Output Format
Return a function that applies the given functions from right to left and produces the final result.
Constraints
- The number of functions may be zero.
- Each function is callable with one argument.
- Preserve the order of composition: the last function in the array runs first.
- When there are no functions, return the input unchanged.
Example 1
Input
functions = [x => x + 1, x => x * 2, x => x - 3], x = 4
Output
3
Explanation
Apply the functions from right to left: first x - 3 gives 1, then x * 2 gives 2, then x + 1 gives 3.
Example 2
Input
functions = [], x = 10
Output
10
Explanation
With no functions to compose, the returned function acts like the identity function.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.