Skip to content

Commit 852ee18

Browse files
committed
minor
1 parent 3d4fa7c commit 852ee18

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

1-js/99-js-misc/03-currying-partials/article.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ As you can see, the implementation is straightforward: it's just two wrappers.
4040

4141
- The result of `curry(func)` is a wrapper `function(a)`.
4242
- When it is called like `sum(1)`, the argument is saved in the Lexical Environment, and a new wrapper is returned `function(b)`.
43-
- Then `sum(1)(2)` finally calls `function(b)` providing `2`, and it passes the call to the original multi-argument `sum`.
43+
- Then this wrapper is called with `2` as an argument, and it passes the call to the original `sum`.
4444

4545
More advanced implementations of currying, such as [_.curry](https://lodash.com/docs#curry) from lodash library, return a wrapper that allows a function to be called both normally and partially:
4646

@@ -73,10 +73,15 @@ Let's curry it!
7373
log = _.curry(log);
7474
```
7575

76-
After that `log` work both the normal way and in the curried form:
76+
After that `log` work normally:
77+
78+
```js
79+
log(new Date(), "DEBUG", "some debug"); // log(a, b, c)
80+
```
81+
82+
...But also works in the curried form:
7783

7884
```js
79-
log(new Date(), "DEBUG", "some debug"); // log(a,b,c)
8085
log(new Date())("DEBUG")("some debug"); // log(a)(b)(c)
8186
```
8287

@@ -102,11 +107,11 @@ debugNow("message"); // [HH:mm] DEBUG message
102107

103108
So:
104109
1. We didn't lose anything after currying: `log` is still callable normally.
105-
2. We were able to generate partial functions such as for today's logs.
110+
2. We can easily generate partial functions such as for today's logs.
106111

107112
## Advanced curry implementation
108113

109-
In case you'd like to get in details, here's the "advanced" curry implementation that we could use above.
114+
In case you'd like to get in details, here's the "advanced" curry implementation for multi-argument functions that we could use above.
110115

111116
It's pretty short:
112117

@@ -142,7 +147,7 @@ alert( curriedSum(1)(2)(3) ); // 6, full currying
142147

143148
The new `curry` may look complicated, but it's actually easy to understand.
144149

145-
The result of `curry(func)` is the wrapper `curried` that looks like this:
150+
The result of `curry(func)` call is the wrapper `curried` that looks like this:
146151

147152
```js
148153
// func is the function to transform
@@ -157,7 +162,7 @@ function curried(...args) {
157162
};
158163
```
159164

160-
When we run it, there are two execution branches:
165+
When we run it, there are two `if` execution branches:
161166

162167
1. Call now: if passed `args` count is the same as the original function has in its definition (`func.length`) or longer, then just pass the call to it.
163168
2. Get a partial: otherwise, `func` is not called yet. Instead, another wrapper `pass` is returned, that will re-apply `curried` providing previous arguments together with the new ones. Then on a new call, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
@@ -173,7 +178,9 @@ For the call `curried(1)(2)(3)`:
173178
If that's still not obvious, just trace the calls sequence in your mind or on the paper.
174179

175180
```smart header="Fixed-length functions only"
176-
The currying requires the function to have a known fixed number of arguments.
181+
The currying requires the function to have a fixed number of arguments.
182+
183+
A function that uses rest parameters, such as `f(...args)`, can't be curried this way.
177184
```
178185

179186
```smart header="A little more than currying"

0 commit comments

Comments
 (0)