Create Curry Function
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Question
In mathematics and computer science, currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument. Currying provides a way for working with functions that take multiple arguments, and using them in frameworks where functions might take only one argument.
In programming, currying is an advanced technique of working with functions. It’s used not only in JavaScript, but in other languages as well.
It is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c). Currying doesn’t call a function. It just transforms it.
Currying is a very useful technique because it helps to avoid passing the same variable again and again, but it also helps to create a higher order function.
Currying transforms a function with multiple arguments into a sequence/series of functions each taking a single argument.
Your task would be to implement a curry() function, which accepts a function and return a curried one.
For example:
Here is an example
const joinArgs = (a, b, c) => {
return `${a}_${b}_${c}`
}
const curriedFunc = curry(joinArgs)
curriedJoin(1, 2, 3) // '1_2_3'
curriedJoin(1)(2, 3) // '1_2_3'
curriedJoin(1, 2)(3) // '1_2_3'

Solution
Solution
Our solution would be implementing the function curry() that will accept one parameter (a function) as an argument.
In this method we are going to return another function called curriedFunc() that will gather the arguments in an array, and this function itself will return another function depending on the case.
function curry(func) {
return function curriedFunc(...args) { }
}
First, we are going to check if the number of args provided equal the number of the args that the function expects, and if so, we are going to spread the args elements and pass them into a function.
if (args.length >= func.length) {
return func(...args);
}
If the number of args provided does not equal the expected number, we are going to return a function that collects the next arguments and recursively calls curriedFunc(), passing it those next arguments.
else {
return function (...next) {
return curriedFunc(...args, ...next);
};
}
This being said, our final solution would look like the following:
function curry(func) {
// ...args collects arguments as array
return function curriedFunc(...args) {
// Check if current args passed equals the number of args func expects
if (args.length >= func.length) {
// if args length equals the expected number, pass into func (spread)
return func(...args);
} else {
/* Else, we return a function that collects the next arguments passed and
recursively call curriedFunc */
return function (...next) {
return curriedFunc(...args, ...next);
};
}
};
}
One Pager Cheat Sheet
One Pager Cheat Sheet
-
Currying is a transformation of functions that translates a function from callable asf(a, b, c)into a sequence of callable functions asf(a)(b)(c), which helps to avoid passing the same variable multiple times and create higher order functions. - We are creating a function called
currythat will return another function,curriedFunc, that will either spread the arguments provided or recursively call itself, depending on the case.