{ "data": { "question": { "questionId": "2740", "questionFrontendId": "2632", "boundTopicId": null, "title": "Curry", "titleSlug": "curry", "content": "

Given a function fn, return a curried version of that function.

\n\n

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\n

In 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\n

 

\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\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 15, "dislikes": 2, "isLiked": null, "similarQuestions": "[{\"title\": \"Memoize\", \"titleSlug\": \"memoize\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Memoize II\", \"titleSlug\": \"memoize-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "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[[]]", "categoryTitle": "JavaScript", "contributors": [], "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 */\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 */\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"286\", \"totalSubmission\": \"326\", \"totalAcceptedRaw\": 286, \"totalSubmissionRaw\": 326, \"acRate\": \"87.7%\"}", "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, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For 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.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }