Partial Function with Placeholders Problem
#2797
Description
LeetCode Problem 2797.
Given a function fn and an array args, return a function partialFn.
Placeholders "_" in the args should be replaced with values from restArgs starting from index 0. Any remaining values in the restArgs should be added at the end of the args.
partialFn should return a result of fn. fn should be called with the elements of the modified args passed as separate arguments.
Example 1:
Input: fn = (...args) => args, args = [2,4,6], restArgs = [8,10]
Output: [2,4,6,8,10]
Explanation:
const partialFn = partial(fn, args)
const result = partialFn(...restArgs)
console.log(result) // [2,4,6,8,10]
There are no placeholders "_" in args therefore restArgs is just added at the end of args. Then the elements of the args are passed as separate arguments to fn, which returns passed arguments as an array.
Example 2:
Input: fn = (...args) => args, args = [1,2,"_",4,"_",6], restArgs = [3,5]
Output: [1,2,3,4,5,6]
Explanation:
const partialFn = partial(fn, args)
const result = partialFn(...restArgs)
console.log(result) // [1,2,3,4,5,6]
Placeholders "_" are replaced with values from the restArgs. Then the elements of the args are passed as separate arguments to fn, which returns passed arguments as an array.
Example 3:
Input: fn = (a, b, c) => b + a - c, args = ["_", 5], restArgs = [5, 20]
Output: -10
Explanation:
const partialFn = partial(fn, args)
const result = partialFn(...restArgs)
console.log(result) // -10
Placeholder "_" is replaced with 5 and 20 is added at the end of args. Then the elements of the args are passed as separate arguments to fn, which returns -10 (5 + 5 - 20).
Constraints:
fnis a functionargsandrestArgsare valid JSON arrays1 <= args.length <= 5 * 10^41 <= restArgs.length <= 5 * 10^40 <= number of placeholders <= restArgs.length
Solution
Approach: Merge the arguments on every call
partial returns a function that, when called with restArgs, builds the final argument list and calls fn with it.
- Walk through
args. A"_"is replaced by the next unused value ofrestArgs; any other value is copied as is. - Whatever is left in
restArgsafter that is appended at the end. - Build a new array on every call. Writing into
argsitself (as a quick solution might) would make the second call of the samepartialFnsee no placeholders any more.
The input guarantees there are no more placeholders than restArgs.
Complexity: Time O(a + r) per call for a = args.length, r = restArgs.length, space O(a + r).
/**
* @param {Function} fn
* @param {Array} args
* @return {Function}
*/
var partial = function (fn, args) {
return function (...restArgs) {
const finalArgs = [];
let next = 0;
for (const arg of args) {
finalArgs.push(arg === '_' ? restArgs[next++] : arg);
}
while (next < restArgs.length) {
finalArgs.push(restArgs[next++]);
}
return fn(...finalArgs);
};
};