-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctionComposition.js
52 lines (45 loc) · 1.62 KB
/
functionComposition.js
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
45
46
47
48
49
50
51
52
// Approach
// To do the function composition, it uses the reduce method on the
// array of functions. The initial value for the accumulator is x,
// the argument passed to the new function returned by compose.
// The reduce method then applies each function in the array to
// the accumulator in turn, returning the final result.
//
// Also the compose function applies the array of functions from
// right to left, where the last function in the array is applied
// first and the first function is applied last.
// This is because the output of the last function in the array is
// the input for the second to last function, and so on, until the
// first function is applied to the original input.
// This order of function application is consistent with the definition
// of function composition, where the innermost function is applied first.
/**
* @param {Function[]} functions
* @return {Function}
*/
var compose = function(functions) {
// Return a new function that takes an input
return function(x) {
// Use the "reduceRight" method to apply the functions in the array from right to left
return functions.reduceRight(function(acc, currentFunction) {
// Call each function with the accumulator as its input
return currentFunction(acc);
},x);
};
};
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/
// Using Arrow Function
/**
* @param {Function[]} functions
* @return {Function}
*/
var compose = function(functions) {
return x => functions.reduceRight((acc,f)=>f(acc),x)
};
/**
* const fn = compose([x => x + 1, x => 2 * x])
* fn(4) // 9
*/