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

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
NumberFunctions / MethodsGoldman SachsAdobe
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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

Steps to solve by this approach:

 Step 1: Create curry function that accepts a function as parameter
 Step 2: Return a curried function that accepts variable number of arguments using rest parameters
 Step 3: Check if number of provided arguments is greater than or equal to original function's arity
 Step 4: If enough arguments are provided, execute original function with all arguments
 Step 5: If not enough arguments, return new function that accepts more arguments
 Step 6: Use concat to combine previous arguments with new arguments
 Step 7: Apply recursive approach to handle multiple levels of currying

Submitted Code :