2018-10-10 18:03:03 -04:00
---
id: 587d7dab367417b2b2512b70
2021-02-06 04:42:36 +00:00
title: Introduction to Currying and Partial Application
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-05 16:38:04 +08:00
forumTopicId: 301232
2021-01-13 03:31:00 +01:00
dashedName: introduction-to-currying-and-partial-application
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
The < dfn > arity< / dfn > of a function is the number of arguments it requires. < dfn > Currying< / dfn > a function means to convert a function of N arity into N functions of arity 1.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
In other words, it restructures a function so it takes one argument, then returns another function that takes the next argument, and so on.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Here's an example:
2020-08-05 16:38:04 +08:00
```js
//Un-curried function
function unCurried(x, y) {
return x + y;
}
2021-02-06 04:42:36 +00:00
//Curried function
2020-08-05 16:38:04 +08:00
function curried(x) {
return function(y) {
return x + y;
}
}
//Alternative using ES6
const curried = x => y => x + y
2021-02-06 04:42:36 +00:00
curried(1)(2) // Returns 3
2020-08-05 16:38:04 +08:00
```
2021-02-06 04:42:36 +00:00
This is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available. Here's an example using the curried function in the example above:
2020-08-05 16:38:04 +08:00
```js
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
```
2021-02-06 04:42:36 +00:00
Similarly, < dfn > partial application< / dfn > can be described as applying a few arguments to a function at a time and returning another function that is applied to more arguments. Here's an example:
2020-08-05 16:38:04 +08:00
```js
//Impartial function
function impartial(x, y, z) {
return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Fill in the body of the `add` function so it uses currying to add parameters `x` , `y` , and `z` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`add(10)(20)(30)` should return `60` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(add(10)(20)(30) === 60);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`add(1)(2)(3)` should return `6` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(add(1)(2)(3) === 6);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`add(11)(22)(33)` should return `66` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(add(11)(22)(33) === 66);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your code should include a final statement that returns `x + y + z` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/[xyz]\s*?\+\s*?[xyz]\s*?\+\s*?[xyz]/g));
2018-10-10 18:03:03 -04:00
```
2020-08-05 16:38:04 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function add(x) {
// Only change code below this line
// Only change code above this line
}
add(10)(20)(30);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
const add = x => y => z => x + y + z
```