1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/originData/curry.json
2023-04-23 22:41:08 +08:00

62 lines
11 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"data": {
"question": {
"questionId": "2740",
"questionFrontendId": "2632",
"categoryTitle": "JavaScript",
"boundTopicId": 2222271,
"title": "Curry",
"titleSlug": "curry",
"content": "<p>Given a function&nbsp;<code>fn</code>,&nbsp;return&nbsp;a&nbsp;<strong>curried</strong>&nbsp;version of that function.</p>\n\n<p>A&nbsp;<strong>curried</strong>&nbsp;function is a function that accepts fewer or an equal number of&nbsp;parameters as the original function and returns either another&nbsp;<strong>curried</strong>&nbsp;function or the same value the original function would have returned.</p>\n\n<p>In practical terms, if you called the original function like&nbsp;<code>sum(1,2,3)</code>, you would call the&nbsp;<strong>curried</strong>&nbsp;version like <code>csum(1)(2)(3)<font face=\"sans-serif, Arial, Verdana, Trebuchet MS\">,&nbsp;</font></code><code>csum(1)(2,3)</code>,&nbsp;<code>csum(1,2)(3)</code>, or&nbsp;<code>csum(1,2,3)</code>. All these methods of calling the <strong>curried</strong> function&nbsp;should return the same value as the original.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1],[2],[3]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\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</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1,2],[3]]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\ncurriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[],[],[1,2,3]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\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</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function life() { return 42; }\ninputs = [[]]\n<strong>Output:</strong> 42\n<strong>Explanation:</strong>\ncurrying a function that accepts zero parameters should effectively do nothing.\ncurriedLife() === 42\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= inputs.length &lt;= 1000</code></li>\n\t<li><code>0 &lt;= inputs[i][j] &lt;= 10<sup>5</sup></code></li>\n\t<li><code>0 &lt;= fn.length &lt;= 1000</code></li>\n\t<li><code>inputs.flat().length == fn.length</code></li>\n\t<li><code>function parameters explicitly defined</code></li>\n</ul>\n",
"translatedTitle": "柯里化",
"translatedContent": "<p>请你编写一个函数,它接收一个其他的函数,并返回该函数的&nbsp;<strong>柯里化&nbsp;</strong>后的形式。</p>\n\n<p><strong>柯里化&nbsp;</strong>函数的定义是接受与原函数相同数量或更少数量的参数,并返回另一个 <strong>柯里化</strong> 后的函数或与原函数相同的值。</p>\n\n<p>实际上,当你调用原函数,如 <code>sum(1,2,3)</code>&nbsp;时,它将调用 <strong>柯里化</strong> 函数的某个形式,如 <code>csum(1)(2)(3)</code> <code>csum(1)(2,3)</code> <code>csum(1,2)(3)</code>,或 <code>csum(1,2,3)</code> 。所有调用 <strong>柯里化</strong> 函数的方法都应该返回与原始函数相同的值。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1</strong></p>\n\n<pre>\n<b>输入:</b>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1],[2],[3]]\n<b>输出:</b>6\n<strong>解释:</strong>\n执行的代码是\nconst curriedSum = curry(fn);\ncurriedSum(1)(2)(3) === 6;\ncurriedSum(1)(2)(3) 应该返回像原函数 sum(1, 2, 3) 一样的值。\n</pre>\n\n<p><strong>示例 2</strong></p>\n\n<pre>\n<strong>输入:</strong>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1,2],[3]]]\n<b>输出:</b>6\n<strong>解释:</strong>\ncurriedSum(1, 2)(3) 应该返回像原函数 sum(1, 2, 3) 一样的值。</pre>\n\n<p><strong>示例 3</strong></p>\n\n<pre>\n<strong>输入:</strong>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[],[],[1,2,3]]\n<b>输出:</b>6\n<strong>解释:</strong>\n你应该能够以任何方式传递参数包括一次性传递所有参数或完全不传递参数。\ncurriedSum()()(1, 2, 3) 应该返回像原函数 sum(1, 2, 3) 一样的值。\n</pre>\n\n<p><strong>示例 4</strong></p>\n\n<pre>\n<strong>输入:</strong>\nfn = function life() { return 42; }\ninputs = [[]]\n<b>输出:</b>42\n<strong>解释:</strong>\n柯里化一个没有接收参数没做有效操作的函数。\ncurriedLife() === 42\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= inputs.length &lt;= 1000</code></li>\n\t<li><code>0 &lt;= inputs[i][j] &lt;= 10<sup>5</sup></code></li>\n\t<li><code>0 &lt;= fn.length &lt;= 1000</code></li>\n\t<li><code>inputs.flat().length == fn.length</code></li>\n\t<li><code>函数参数需要被显式定义</code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 2,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": false, \"java\": true, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Function} fn\n * @return {Function}\n */\nvar curry = function(fn) {\n return function curried() {\n\n };\n};\n\n/**\n * function sum(a, b) { return a + b; }\n * const csum = curry(sum);\n * csum(1)(2) // 3\n */",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function curry(fn: Function): Function {\n return function curried() {\n\n };\n};\n\n/**\n * function sum(a, b) { return a + b; }\n * const csum = curry(sum);\n * csum(1)(2) // 3\n */",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"175\", \"totalSubmission\": \"202\", \"totalAcceptedRaw\": 175, \"totalSubmissionRaw\": 202, \"acRate\": \"86.6%\"}",
"hints": [
"You can access the count of parameters expected to passed into a function with \"fn.length\".",
"You can use recursion. If the length of params passed is equal to fn.length, you are done. Just pass those params to fn. Otherwise return a function that is includes the previous passed params plus the new params. The new function should contain a recursive call to curry()."
],
"solution": null,
"status": null,
"sampleTestCase": "function sum(a, b, c) { return a + b + c; }\n[[1],[2],[3]]",
"metaData": "{\n \"name\": \"curry\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"inputs\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> 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"
}
}
}