Skip to content

Commit 509fc00

Browse files
committed
exec: what-is-currying-and-how-does-it-work
1 parent 1f02204 commit 509fc00

File tree

1 file changed

+20
-8
lines changed
  • questions/what-is-currying-and-how-does-it-work

1 file changed

+20
-8
lines changed

questions/what-is-currying-and-how-does-it-work/en-US.mdx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: What is currying and how does it work?
66

77
Currying is a technique in functional programming where a function that takes multiple arguments is transformed into a series of functions that each take a single argument. This allows for partial application of functions. For example, a function `f(a, b, c)` can be curried into `f(a)(b)(c)`. Here's a simple example in JavaScript:
88

9-
```js
9+
```js live
1010
function add(a) {
1111
return function (b) {
1212
return function (c) {
@@ -16,8 +16,13 @@ function add(a) {
1616
}
1717

1818
const addOne = add(1);
19+
console.log(addOne); // function object
20+
1921
const addOneAndTwo = addOne(2);
20-
const result = addOneAndTwo(3); // result is 6
22+
console.log(addOneAndTwo); // function object
23+
24+
const result = addOneAndTwo(3);
25+
console.log(result); // Output: 6
2126
```
2227

2328
---
@@ -37,7 +42,7 @@ Currying is a functional programming technique where a function with multiple ar
3742

3843
Here's a simple example to illustrate currying in JavaScript:
3944

40-
```js
45+
```js live
4146
// Non-curried function
4247
function add(a, b, c) {
4348
return a + b + c;
@@ -54,8 +59,13 @@ function curriedAdd(a) {
5459

5560
// Using the curried function
5661
const addOne = curriedAdd(1);
62+
console.log(addOne); // function object
63+
5764
const addOneAndTwo = addOne(2);
58-
const result = addOneAndTwo(3); // result is 6
65+
console.log(addOneAndTwo); // function object
66+
67+
const result = addOneAndTwo(3);
68+
console.log(result); // Output: 6
5969
```
6070

6171
### Benefits of currying
@@ -68,7 +78,7 @@ const result = addOneAndTwo(3); // result is 6
6878

6979
Consider a function that calculates the volume of a rectangular prism:
7080

71-
```js
81+
```js live
7282
function volume(length, width, height) {
7383
return length * width * height;
7484
}
@@ -85,19 +95,21 @@ function curriedVolume(length) {
8595
// Using the curried function
8696
const volumeWithLength5 = curriedVolume(5);
8797
const volumeWithLength5AndWidth4 = volumeWithLength5(4);
88-
const result = volumeWithLength5AndWidth4(3); // result is 60
98+
const result = volumeWithLength5AndWidth4(3);
99+
console.log(result); // Output: 60
89100
```
90101

91102
### Currying with arrow functions
92103

93104
You can also use arrow functions to make the syntax more concise:
94105

95-
```js
106+
```js live
96107
const curriedAdd = (a) => (b) => (c) => a + b + c;
97108

98109
const addOne = curriedAdd(1);
99110
const addOneAndTwo = addOne(2);
100-
const result = addOneAndTwo(3); // result is 6
111+
const result = addOneAndTwo(3);
112+
console.log(result); // Output: 6
101113
```
102114

103115
## Further reading

0 commit comments

Comments
 (0)