#2629
Easy JavaScript Function composition
86.9% acceptance
Mar 2, 2026
826
64
Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function composition of the array of functions.
The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).
The function composition of an empty list of functions is the identity function f(x) = x.
You may assume each function in the array accepts one integer as input and returns one integer as output.
Solution
TypeScript
Time O(1)
Space O(1)
type F = (x: number) => number;
function compose(functions: F[]): F {
return function (x: number): number {
return functions.reduceRight((acc, fn) => fn(acc), x);
};
}
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/