HomePremium · ₹1199
← All questions

Implement a curry utility

Medium
Was this asked in an interview?

Write a generic curry(fn) helper that transforms any function into a curried version.

Problem

Write a generic curry(fn) that returns a curried version of fn.

You may call the curried function one argument at a time or pass several arguments in one call. Once enough arguments are collected (matching fn.length), invoke fn and return its result.

Input

const sum3 = (a, b, c) => a + b + c;
const curried = curry(sum3);

curried(1)(2)(3);
curried(1, 2)(3);
curried(1)(2, 3);

Expected output

All three calls return 6.

Implement:

function curry(fn) {
  // Your code here
}
Implement a curry utility — JavaScript Interview Question | Mentoxis