
Problem
Solution
Submissions
Curry Function
Certification: Intermediate Level
Accuracy: 0%
Submissions: 3
Points: 5
Write a JavaScript program to implement a curry function that transforms a regular function into a curried function. Currying is a technique where a function with multiple arguments is transformed into a sequence of functions, each taking a single argument. The curried function should be able to be called with arguments one at a time and return the final result when all arguments are provided.
Example 1
- Input: Original function: add(a, b, c) = a + b + c, Curried version: curriedAdd(1)(2)(3)
- Output: 6
- Explanation:
- Original function add takes three parameters a, b, c.
- Curry function transforms add into curriedAdd.
- curriedAdd(1) returns a function waiting for remaining arguments.
- curriedAdd(1)(2) returns a function waiting for the last argument.
- curriedAdd(1)(2)(3) provides all arguments and returns 6.
- The computation is 1 + 2 + 3 = 6.
- Original function add takes three parameters a, b, c.
Example 2
- Input: Original function: multiply(a, b, c, d) = a * b * c * d, Curried version: curriedMultiply(2)(3)(4)(5)
- Output: 120
- Explanation:
- Original function multiply takes four parameters a, b, c, d.
- Curry function transforms multiply into curriedMultiply.
- Each call curriedMultiply(2), curriedMultiply(2)(3), etc.
- returns partial application.
- curriedMultiply(2)(3)(4)(5) provides all four arguments.
- The computation is 2 * 3 * 4 * 5 = 120.
- Original function multiply takes four parameters a, b, c, d.
Constraints
- The curry function should work with functions of any arity (number of parameters)
- The original function can have 1 to 10 parameters
- Arguments can be numbers, strings, or other primitive types
- The curried function should support partial application
- Time Complexity: O(1) for each function call
- Space Complexity: O(n) where n is the number of parameters
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use function length property to determine the number of expected arguments
- Keep track of accumulated arguments using closures
- Return a new function if more arguments are needed
- Execute the original function when all arguments are collected
- Use rest parameters and spread operator for handling variable arguments
- Implement recursive approach to handle nested function calls