Is there a problem with curryN?
xuanWangisAlibaba19960403 opened this issue · comments
const curriedAdd = R.curryN(3, (a, b, c) => { return a + b + c })
console.log(curriedAdd(1, R.__, R.__, R.__, R.__, R.__)(R.__, R.__)(3, R.__)(R.__)(4))
I understand that the result should be returned at this time, but the function is returned
Should this be adjusted?
while (combinedIdx < received.length || argsIdx < arguments.length) {
var result;
if (combinedIdx < received.length && (!_isPlaceholder(received[combinedIdx]) || argsIdx >= arguments.length)) {
result = received[combinedIdx];
} else {
result = arguments[argsIdx];
argsIdx += 1;
}
if (!_isPlaceholder(result)) {
left -= 1;
combined[combinedIdx] = result;
combinedIdx += 1;
} else {
hasPlaceholder = true;
}
}
calling a function fn = curryN(N, f)
with the placeholder __
as (N+1)th argument certainly breaks fn
.
In good JS fashion extra arguments to a function should be ignored — but __
seems to be an extreme edge case.
Is that worth fixing?
i.e What is the use case in your code?
I think this doesn't matter
function _curryN(length, received, fn) {
return function () {
var combined = [];
var argsIdx = 0;
var left = length;
var combinedIdx = 0;
var hasPlaceholder = false;
while (combinedIdx < received.length || argsIdx < arguments.length) {
var result;
if (combinedIdx < received.length && (!_isPlaceholder(received[combinedIdx]) || argsIdx >= arguments.length)) {
result = received[combinedIdx];
} else {
result = arguments[argsIdx];
argsIdx += 1;
}
combined[combinedIdx] = result;
if (!_isPlaceholder(result)) {
left -= 1;
} else {
hasPlaceholder = true;
}
combinedIdx += 1;
}
return !hasPlaceholder && left <= 0 ? fn.apply(this, combined) : _arity(Math.max(0, left), _curryN(length, combined, fn));
};
}
Why not eliminate the placeholder in this function execution, but in the next execution? What are the considerations?
Do you have a valid use case for this @xuanWangisAlibaba19960403 ? I can't really think of one, but if you have a fix which does not degrade performance we can consider a PR.