-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2629-function-composition.js
More file actions
76 lines (63 loc) · 2.34 KB
/
Copy path2629-function-composition.js
File metadata and controls
76 lines (63 loc) · 2.34 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Blog: https://allenliservice.online/leetcode-javascript-30-days-challenge-day7-2629-function-composition/
// <strong>solution:</strong>
// 這題最重要的是了解題目的說明,題目將條件依序放到陣列中,
// 同時希望我們由右到左的讀取陣列函式的內容,
// 這個過程我們可以使用reduceRight()來完成指定功能,如下:
// <strong>Code 1: BigO(n)</strong>
var compose = function (functions) {
return function (x) {
return functions.reduceRight((acc, cur) => cur(acc), x);
};
};
/* <strong>FlowChart:</strong>
<strong>Example 1</strong>
<pre style='background-color:#ggg'>
Input: functions = [x => x + 1, x => x * x, x => 2 * x], x = 4
functions.reduceRight((acc, cur) => cur(acc), x)
functions.reduceRight((acc, cur) => cur(2 * 4), 4) //acc = 8
functions.reduceRight((acc, cur) => cur(8 * 8), 8) //acc = 64
functions.reduceRight((acc, cur) => cur(64 + 1), 64) //acc = 65
return 65;
</pre>
<strong>Example 2</strong>
<pre style='background-color:#ggg'>
Input: functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1
functions.reduceRight((acc, cur) => cur(acc), x)
functions.reduceRight((acc, cur) => cur(10 * 1), 1) //acc = 10
functions.reduceRight((acc, cur) => cur(10 * 10), 10) //acc = 100
functions.reduceRight((acc, cur) => cur(10 + 100), 100) //acc = 1000
return 1000;
</pre>
<strong>Example 3</strong>
<pre style='background-color:#ggg'>
Input: functions = [], x = 42
functions.reduceRight((acc, cur) => cur(acc), x)
functions.reduceRight((acc, cur) => cur(acc), 42)
return 42;
</pre> */
// <strong>Code 2: BigO(n)</strong>
var compose = function (functions) {
return function (x) {
return functions.reverse().reduce((acc, cur) => cur(acc), x);
};
};
// <strong>Code 3: BigO(n)</strong>
var compose = function (functions) {
return function (x) {
while (functions.length) {
x = functions.pop()(x);
}
return x;
};
};
// <strong>Code 4: BigO(n)</strong>
var compose = function (functions) {
return function recursion(x) {
//如果funcitons中沒有陣列元素,則回傳x的值。
if (functions.length === 0) return x;
//*重點,將x重新賦值為:移除functions中最右邊的陣列的同時,帶入(x)的參數進去執行取得結果。
x = functions.pop()(x);
//重複呼叫遞迴函式直到陣列中無元素。
return recursion(x);
};
};