2018-09-30 23:01:58 +01:00
---
id: 587d7dab367417b2b2512b70
title: Introduction to Currying and Partial Application
challengeType: 1
2020-05-21 17:31:25 +02:00
isHidden: false
2019-08-05 09:17:33 -07:00
forumTopicId: 301232
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
2019-10-27 15:45:37 -01: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.
2018-09-30 23:01:58 +01: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.
Here's an example:
2019-05-17 06:20:30 -07:00
```js
//Un-curried function
function unCurried(x, y) {
return x + y;
}
//Curried function
function curried(x) {
return function(y) {
return x + y;
}
}
//Alternative using ES6
const curried = x => y => x + y
curried(1)(2) // Returns 3
```
2019-10-27 15:45:37 -01: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:
2019-05-17 06:20:30 -07:00
```js
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
```
2019-10-27 15:45:37 -01: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.
2018-09-30 23:01:58 +01:00
Here's an example:
2019-05-17 06:20:30 -07:00
```js
//Impartial function
function impartial(x, y, z) {
return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13
```
2018-09-30 23:01:58 +01:00
</section>
## Instructions
<section id='instructions'>
Fill in the body of the <code>add</code> function so it uses currying to add parameters <code>x</code>, <code>y</code>, and <code>z</code>.
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: <code>add(10)(20)(30)</code> should return <code>60</code>.
2019-07-24 01:47:32 -07:00
testString: assert(add(10)(20)(30) === 60);
2018-10-04 14:37:37 +01:00
- text: <code>add(1)(2)(3)</code> should return <code>6</code>.
2019-07-24 01:47:32 -07:00
testString: assert(add(1)(2)(3) === 6);
2018-10-04 14:37:37 +01:00
- text: <code>add(11)(22)(33)</code> should return <code>66</code>.
2019-07-24 01:47:32 -07:00
testString: assert(add(11)(22)(33) === 66);
2018-10-04 14:37:37 +01:00
- text: Your code should include a final statement that returns <code>x + y + z</code>.
2019-07-24 01:47:32 -07:00
testString: assert(code.match(/[xyz]\s*?\+\s*?[xyz]\s*?\+\s*?[xyz]/g));
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function add(x) {
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-10-08 01:01:53 +01:00
2020-03-08 07:46:28 -07:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
}
add(10)(20)(30);
```
</div>
</section>
## Solution
<section id='solution'>
```js
2018-10-16 10:43:02 +11:00
const add = x => y => z => x + y + z
2018-09-30 23:01:58 +01:00
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
</section>