Skip to content

Commit 2027939

Browse files
committed
minor fixes
1 parent 6113f33 commit 2027939

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ function curry(func) {
121121
return function curried(...args) {
122122
if (args.length >= func.length) {
123123
return func.apply(this, args);
124-
} else return curried.bind(this, ...args);
124+
} else {
125+
return curried.bind(this, ...args);
126+
}
125127
};
126128

127129
}
@@ -150,24 +152,18 @@ The result of `curry(func)` call is the wrapper `curried` that looks like this:
150152
function curried(...args) {
151153
if (args.length >= func.length) { // (1)
152154
return func.apply(this, args);
153-
} else return curried.bind(this, ...args); // (2)
155+
} else {
156+
return curried.bind(this, ...args); // (2)
157+
}
154158
};
155159
```
156160

157161
When we run it, there are two `if` execution branches:
158162

159-
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.
160-
2. Get a partial: otherwise, `func` is not called yet. Instead, a new bounded function using curried is returned, that takes the `...args` i.e. the current arguments as pre-specified. Then on a new call, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
163+
1. If passed `args` count is the same or more than the original function has in its definition (`func.length`) , then just pass the call to it using `func.apply`.
164+
2. Otherwise, get a partial: we don't call `func` just yet. Instead, `curried.bind(this, ...args)` returns a "bound" function: the same as `curried`, but with pre-set `this` and pre-specified arguments. In other words, this is exactly where the partial variant of function is created, with first arguments fixed.
161165

162-
For instance, let's see what happens in the case of `sum(a, b, c)`. Three arguments, so `sum.length = 3`.
163-
164-
For the call `curried(1)(2)(3)`:
165-
166-
1. The first call `curried(1)` returns a new bounded `curried` with `1` as pre-specified argument.
167-
2. The bounded `curried` is called with `(2)`: it takes previous args (`1`) due to bind, and new leading argument `(2)` and calls `curried(2)`. As the argument count is still less than 3, `curry` returns new bounded `curried` with (`1`, `2`) as pre-specified arguments.
168-
3. The bounded `curried` is called again with `(3)`, for the next call `curried(3)` takes previous args (`1`, `2`) and new leading argument `3`, making the call `curried(3)` -- there are `3` arguments at last, they are given to the original function.
169-
170-
If that's still not obvious, just trace the calls sequence in your mind or on paper.
166+
Then, if we call it, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
171167

172168
```smart header="Fixed-length functions only"
173169
The currying requires the function to have a fixed number of arguments.

0 commit comments

Comments
 (0)