Good morning! Here's our prompt for today.
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
1const joinArgs = (a, b, c) => {
2 return `${a}_${b}_${c}`
3}
4
5const curriedFunc = curry(joinArgs)
6
7curriedJoin(1, 2, 3) // '1_2_3'
8curriedJoin(1)(2, 3) // '1_2_3'
9curriedJoin(1, 2)(3) // '1_2_3'

Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
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);
};
}
};
}
We'll now take you through what you need to know.
How do I use this guide?
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.
1function curry(func) {
2 return function curriedFunc(...args) { }
3}
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.
1if (args.length >= func.length) {
2 return func(...args);
3}
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.
1else {
2 return function (...next) {
3 return curriedFunc(...args, ...next);
4 };
5}
This being said, our final solution would look like the following:
1function curry(func) {
2 // ...args collects arguments as array
3 return function curriedFunc(...args) {
4 // Check if current args passed equals the number of args func expects
5 if (args.length >= func.length) {
6 // if args length equals the expected number, pass into func (spread)
7 return func(...args);
8 } else {
9 /* Else, we return a function that collects the next arguments passed and
10 recursively call curriedFunc */
11 return function (...next) {
12 return curriedFunc(...args, ...next);
13 };
14 }
15 };
16}
One Pager Cheat Sheet
Currying is a transformation of functions that translates a function from callable as
f(a, b, c)into a sequence of callable functions as
f(a)(b)(c), which helps to avoid passing the same variable multiple times and create higher order functions.
- We are creating a function called
curry
that will return another function,curriedFunc
, that will either spread the arguments provided or recursively call itself, depending on the case.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx
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);
};
}
};
}
Great job getting through this. Let's move on.
If you had any problems with this tutorial, check out the main forum thread here.