
Problem Statement
The task requires creating a new function from a sequence of given arithmetic functions. This sequence of functions is expressed as an array where each element represents a function. The main goal is to perform function composition; where the output of one function becomes the input to the next one starting from the last function in the array.
For example, with functions [f(x), g(x), h(x)]
in an array, the function composition will be f(g(h(x)))
, where h(x)
is evaluated first, then g(h(x))
, and finally f(g(h(x)))
. If there are no functions in the array, the program should return a default identity function that outputs the same integer passed to it, i.e., f(x) = x
. This identity function ensures that there is a meaningful return value even when there are no operations to perform on x
.
Each function in the array operates exclusively on integers, both accepting and returning an integer.
Examples
Example 1
Input:
functions = [x => x + 1, x => x * x, x => 2 * x] x = 4
Output:
65
Explanation:
Evaluating from right to left: 2 * 4 = 8 8 * 8 = 64 64 + 1 = 65
Example 2
Input:
functions = [x => 10 * x, x => 10 * x, x => 10 * x] x = 1
Output:
1000
Explanation:
10 * 1 = 10 10 * 10 = 100 10 * 100 = 1000
Example 3
Input:
functions = [] x = 42
Output:
42
Explanation:
No functions to apply, so the identity function returns x unchanged.
Constraints
-1000 <= x <= 1000
0 <= functions.length <= 1000
- All functions accept and return a single integer
Approach and Intuition
Function composition deals with chaining multiple functions together in a specific sequence. The output of one function becomes the input of the next, applied in reverse order from how they’re written in the array.
Identity Function Handling:
- If
functions
is empty, return the inputx
unchanged.
- If
Iterative Function Execution:
- Start from the last function in the array.
- Use a loop to apply each function one after the other.
- Continuously update the value of
x
as you go.
Final Evaluation:
- Once all functions have been applied, return the final value of
x
.
- Once all functions have been applied, return the final value of
This guarantees proper chaining of operations and handles edge cases like an empty function array smoothly.
Solutions
- JavaScript
var chainFunctions = function (fnList) {
return function (value) {
if (fnList.length === 0) return value;
let result = value;
for (const singleFn of fnList.reverse()) {
result = singleFn(result);
}
return result;
};
};
The solution provided tackles the problem of function composition in JavaScript, where it combines multiple functions into a single function. The goal is to apply a list of functions to an initial value sequentially, where the output of each function becomes the input for the next.
In this solution:
chainFunctions is defined as a higher-order function that takes an array of functions (fnList) as its argument.
It returns a function that accepts an initial value, which is then processed by the functions in the list.
Inside the returned function:
- Check if the function list is empty; if so, return the initial value immediately.
- The initial value is stored in
result
. - The functions are reversed and applied in sequence using a for loop. Each function operates on and modifies the current
result
. - The final
result
is returned after all functions have been applied.
This approach effectively allows for dynamic function chaining, where the order of operation can be controlled by the order of functions in the input list, and can be easily adapted for various scenarios where sequential function application is needed.
No comments yet.