{ "data": { "question": { "questionId": "2741", "questionFrontendId": "2629", "categoryTitle": "JavaScript", "boundTopicId": 2184269, "title": "Function Composition", "titleSlug": "function-composition", "content": "
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.
\n\n\n
Example 1:
\n\n\nInput: functions = [x => x + 1, x => x * x, x => 2 * x], x = 4\nOutput: 65\nExplanation:\nEvaluating from right to left ...\nStarting with x = 4.\n2 * (4) = 8\n(8) * (8) = 64\n(64) + 1 = 65\n\n\n
Example 2:
\n\n\nInput: functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1\nOutput: 1000\nExplanation:\nEvaluating from right to left ...\n10 * (1) = 10\n10 * (10) = 100\n10 * (100) = 1000\n\n\n
Example 3:
\n\n\nInput: functions = [], x = 42\nOutput: 42\nExplanation:\nThe composition of zero functions is the identity function\n\n
\n
Constraints:
\n\n-1000 <= x <= 1000
0 <= functions.length <= 1000
请你编写一个函数,它接收一个函数数组 [f1, f2, f3,…, fn]
,并返回一个新的函数 fn
,它是函数数组的 复合函数 。
[f(x), g(x), h(x)]
的 复合函数 为 fn(x) = f(g(h(x)))
。
一个空函数列表的 复合函数 是 恒等函数 f(x) = x
。
你可以假设数组中的每个函数接受一个整型参数作为输入,并返回一个整型作为输出。
\n\n\n\n
示例 1:
\n\n\n输入:functions = [x => x + 1, x => x * x, x => 2 * x], x = 4\n输出:65\n解释:\n从右向左计算......\nStarting with x = 4.\n2 * (4) = 8\n(8) * (8) = 64\n(64) + 1 = 65\n\n\n
示例 2:
\n\n\n输出:functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1\n输入:1000\n解释:\n从右向左计算......\n10 * (1) = 10\n10 * (10) = 100\n10 * (100) = 1000\n\n\n
示例 3:
\n\n\n输入:functions = [], x = 42\n输出:42\n解释:\n空函数列表的复合函数就是恒等函数\n\n
\n\n
提示:
\n\n-1000 <= x <= 1000
0 <= functions.length <= 1000
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[x => x + 1, x => x * x, x => 2 * x]\n4\n[x => 10 * x, x => 10 * x, x => 10 * x]\n1\n[]\n42",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n