{ "data": { "question": { "questionId": "2740", "questionFrontendId": "2632", "boundTopicId": null, "title": "Curry", "titleSlug": "curry", "content": "
Given a function fn
, return a curried version of that function.
A curried function is a function that accepts fewer or an equal number of parameters as the original function and returns either another curried function or the same value the original function would have returned.
\n\nIn practical terms, if you called the original function like sum(1,2,3)
, you would call the curried version like csum(1)(2)(3),
csum(1)(2,3)
, csum(1,2)(3)
, or csum(1,2,3)
. All these methods of calling the curried function should return the same value as the original.
\n
Example 1:
\n\n\nInput: \nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1],[2],[3]]\nOutput: 6\nExplanation:\nThe code being executed is:\nconst curriedSum = curry(fn);\ncurriedSum(1)(2)(3) === 6;\ncurriedSum(1)(2)(3) should return the same value as sum(1, 2, 3).\n\n\n
Example 2:
\n\n\nInput:\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1,2],[3]]]\nOutput: 6\nExplanation:\ncurriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).\n\n
Example 3:
\n\n\nInput:\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[],[],[1,2,3]]\nOutput: 6\nExplanation:\nYou should be able to pass the parameters in any way, including all at once or none at all.\ncurriedSum()()(1, 2, 3) should return the same value as sum(1, 2, 3).\n\n\n
Example 4:
\n\n\nInput:\nfn = function life() { return 42; }\ninputs = [[]]\nOutput: 42\nExplanation:\ncurrying a function that accepts zero parameters should effectively do nothing.\ncurriedLife() === 42\n\n\n
\n
Constraints:
\n\n1 <= inputs.length <= 1000
0 <= inputs[i][j] <= 105
0 <= fn.length <= 1000
inputs.flat().length == fn.length
function parameters explicitly defined
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" } } }