{ "data": { "question": { "questionId": "2740", "questionFrontendId": "2632", "categoryTitle": "JavaScript", "boundTopicId": 2222271, "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
请你编写一个函数,它接收一个其他的函数,并返回该函数的 柯里化 后的形式。
\n\n柯里化 函数的定义是接受与原函数相同数量或更少数量的参数,并返回另一个 柯里化 后的函数或与原函数相同的值。
\n\n实际上,当你调用原函数,如 sum(1,2,3)
时,它将调用 柯里化 函数的某个形式,如 csum(1)(2)(3)
, csum(1)(2,3)
, csum(1,2)(3)
,或 csum(1,2,3)
。所有调用 柯里化 函数的方法都应该返回与原始函数相同的值。
\n\n
示例 1:
\n\n\n输入:\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1],[2],[3]]\n输出:6\n解释:\n执行的代码是:\nconst curriedSum = curry(fn);\ncurriedSum(1)(2)(3) === 6;\ncurriedSum(1)(2)(3) 应该返回像原函数 sum(1, 2, 3) 一样的值。\n\n\n
示例 2:
\n\n\n输入:\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1,2],[3]]]\n输出:6\n解释:\ncurriedSum(1, 2)(3) 应该返回像原函数 sum(1, 2, 3) 一样的值。\n\n
示例 3:
\n\n\n输入:\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[],[],[1,2,3]]\n输出:6\n解释:\n你应该能够以任何方式传递参数,包括一次性传递所有参数或完全不传递参数。\ncurriedSum()()(1, 2, 3) 应该返回像原函数 sum(1, 2, 3) 一样的值。\n\n\n
示例 4:
\n\n\n输入:\nfn = function life() { return 42; }\ninputs = [[]]\n输出:42\n解释:\n柯里化一个没有接收参数,没做有效操作的函数。\ncurriedLife() === 42\n\n\n
\n\n
提示:
\n\n1 <= inputs.length <= 1000
0 <= inputs[i][j] <= 105
0 <= fn.length <= 1000
inputs.flat().length == fn.length
函数参数需要被显式定义
\\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 4.5.4<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/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": "function sum(a, b, c) { return a + b + c; }\n[[1],[2],[3]]\nfunction sum(a, b, c) { return a + b + c; }\n[[1,2],[3]]\nfunction sum(a, b, c) { return a + b + c; }\n[[],[],[1,2,3]]\nfunction life() { return 42; }\n[[]]",
"__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