You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: questions/what-is-currying-and-how-does-it-work/en-US.mdx
+20-8Lines changed: 20 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: What is currying and how does it work?
6
6
7
7
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:
8
8
9
-
```js
9
+
```js live
10
10
functionadd(a) {
11
11
returnfunction (b) {
12
12
returnfunction (c) {
@@ -16,8 +16,13 @@ function add(a) {
16
16
}
17
17
18
18
constaddOne=add(1);
19
+
console.log(addOne); // function object
20
+
19
21
constaddOneAndTwo=addOne(2);
20
-
constresult=addOneAndTwo(3); // result is 6
22
+
console.log(addOneAndTwo); // function object
23
+
24
+
constresult=addOneAndTwo(3);
25
+
console.log(result); // Output: 6
21
26
```
22
27
23
28
---
@@ -37,7 +42,7 @@ Currying is a functional programming technique where a function with multiple ar
37
42
38
43
Here's a simple example to illustrate currying in JavaScript:
39
44
40
-
```js
45
+
```js live
41
46
// Non-curried function
42
47
functionadd(a, b, c) {
43
48
return a + b + c;
@@ -54,8 +59,13 @@ function curriedAdd(a) {
54
59
55
60
// Using the curried function
56
61
constaddOne=curriedAdd(1);
62
+
console.log(addOne); // function object
63
+
57
64
constaddOneAndTwo=addOne(2);
58
-
constresult=addOneAndTwo(3); // result is 6
65
+
console.log(addOneAndTwo); // function object
66
+
67
+
constresult=addOneAndTwo(3);
68
+
console.log(result); // Output: 6
59
69
```
60
70
61
71
### Benefits of currying
@@ -68,7 +78,7 @@ const result = addOneAndTwo(3); // result is 6
68
78
69
79
Consider a function that calculates the volume of a rectangular prism:
70
80
71
-
```js
81
+
```js live
72
82
functionvolume(length, width, height) {
73
83
return length * width * height;
74
84
}
@@ -85,19 +95,21 @@ function curriedVolume(length) {
0 commit comments