-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlce_p2629_function_composition.js
More file actions
44 lines (34 loc) · 1 KB
/
lce_p2629_function_composition.js
File metadata and controls
44 lines (34 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
LCE 2629. Function Composition
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.
Constraints:
- -1000 <= x <= 1000
- 0 <= functions.length <= 1000
- all functions accept and return a single integer
*/
/**
* @param {Function[]} functions
* @return {Function}
*/
// conventional
var compose = function (functions) {
return function (x) {
for (i = functions.length - 1; i >= 0; i--) {
x = functions[i](x);
}
return x;
};
};
// Array.reduceRight
var compose = function (functions) {
return function (x) {
return functions.reduceRight((acc, f) => f(acc), x);
};
};
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/