Function Composition - Problem
Function Composition Challenge
In functional programming, function composition is a powerful concept where you combine multiple functions into a single function. Given an array of functions
๐ฏ The Goal: Return a composed function that applies all input functions from right to left. For example, if you have functions
โ ๏ธ Special Case: If the array is empty, return the identity function that simply returns its input unchanged:
Each function in the array accepts one integer and returns one integer. Your composed function should do the same!
In functional programming, function composition is a powerful concept where you combine multiple functions into a single function. Given an array of functions
[f1, f2, f3, ..., fn], your task is to create a new function that represents the composition of all these functions.๐ฏ The Goal: Return a composed function that applies all input functions from right to left. For example, if you have functions
[f(x), g(x), h(x)], the composed function should work like f(g(h(x))) - first apply h, then g, then f.โ ๏ธ Special Case: If the array is empty, return the identity function that simply returns its input unchanged:
f(x) = x.Each function in the array accepts one integer and returns one integer. Your composed function should do the same!
Input & Output
basic_composition.js โ JavaScript
$
Input:
functions = [x => x * 2, x => x + 1, x => x * x], x = 4
โบ
Output:
34
๐ก Note:
The composition works as: multiply_by_2(add_one(square(4))) = multiply_by_2(add_one(16)) = multiply_by_2(17) = 34
single_function.js โ JavaScript
$
Input:
functions = [x => x + 1], x = 5
โบ
Output:
6
๐ก Note:
With only one function, composition simply applies that function: add_one(5) = 6
empty_array.js โ JavaScript
$
Input:
functions = [], x = 42
โบ
Output:
42
๐ก Note:
Empty array returns the identity function, so the input is returned unchanged: identity(42) = 42
Visualization
Tap to expand
Understanding the Visualization
1
Input enters pipeline
Value 4 enters the composition pipeline
2
First transformation
Rightmost function (square) transforms 4 โ 16
3
Second transformation
Middle function (add_one) transforms 16 โ 17
4
Final transformation
Leftmost function (multiply_by_2) transforms 17 โ 34
Key Takeaway
๐ฏ Key Insight: Function composition follows mathematical convention - read from right to left, creating elegant pipelines of transformations
Time & Space Complexity
Time Complexity
O(n)
Still need to process each function once during composition and once during execution
โ Linear Growth
Space Complexity
O(n)
Creates nested function closures, but this is typically optimized by JavaScript engines
โก Linearithmic Space
Constraints
- 0 โค functions.length โค 1000
- All functions accept and return a single integer
- -1000 โค x โค 1000 (input to composed function)
- Functions are applied right-to-left (mathematical composition order)
- Empty function array should return identity function f(x) = x
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code