Mark As Completed Discussion

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

JAVASCRIPT
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'

Question

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

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.

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

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

JAVASCRIPT
1else {
2  return function (...next) {
3    return curriedFunc(...args, ...next);
4  };
5}

This being said, our final solution would look like the following:

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

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Great job getting through this. Let's move on.

If you had any problems with this tutorial, check out the main forum thread here.