{ "data": { "question": { "questionId": "2741", "questionFrontendId": "2629", "boundTopicId": null, "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
all functions accept and return a single integer
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"typescript\": [\"Typescript\", \"TypeScript 4.5.4, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2020 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }