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: 1-js/99-js-misc/03-currying-partials/article.md
+7-3Lines changed: 7 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -122,7 +122,9 @@ function curry(func) {
122
122
if (args.length>=func.length) {
123
123
returnfunc.apply(this, args);
124
124
} else {
125
-
returncurried.bind(this, ...args);
125
+
returnfunction(...args2) {
126
+
returncurried.apply(this, args.concat(args2));
127
+
}
126
128
}
127
129
};
128
130
@@ -153,15 +155,17 @@ function curried(...args) {
153
155
if (args.length>=func.length) { // (1)
154
156
returnfunc.apply(this, args);
155
157
} else {
156
-
returncurried.bind(this, ...args); // (2)
158
+
returnfunction(...args2) { // (2)
159
+
returncurried.apply(this, args.concat(args2));
160
+
}
157
161
}
158
162
};
159
163
```
160
164
161
165
When we run it, there are two `if` execution branches:
162
166
163
167
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.
168
+
2. Otherwise, get a partial: we don't call `func` just yet. Instead, another wrapper is returned, that will re-apply `curried` providing previous arguments together with the new ones.
165
169
166
170
Then, if we call it, again, we'll get either a new partial (if not enough arguments) or, finally, the result.
0 commit comments