Problem

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.

Examples

Example 1:

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
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:

  • fn is a function
  • args and restArgs are valid JSON arrays
  • 1 <= args.length <= 5 * 10^4
  • 1 <= restArgs.length <= 5 * 10^4
  • 0 <= number of placeholders <= restArgs.length

Solution

Method 1 – Simulate Placeholder Replacement

Intuition

We want to create a partial function where placeholders ("_") in the initial argument list are replaced by values from the next call. Any extra values are appended at the end. This is a direct simulation of the described process.

Approach

  1. Return a function that takes ...restArgs.
  2. Iterate through args, replacing each “_” with the next value from restArgs (in order).
  3. If there are leftover values in restArgs, append them to the end.
  4. Call fn with the resulting argument list using spread syntax.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function partial(fn, args) {
    return function(...restArgs) {
        let res = [];
        let idx = 0;
        for (let x of args) {
            if (x === "_" && idx < restArgs.length) {
                res.push(restArgs[idx++]);
            } else {
                res.push(x);
            }
        }
        while (idx < restArgs.length) res.push(restArgs[idx++]);
        return fn(...res);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function partial(fn: (...args: any[]) => any, args: any[]): (...restArgs: any[]) => any {
    return (...restArgs: any[]) => {
        const res: any[] = [];
        let idx = 0;
        for (const x of args) {
            if (x === "_" && idx < restArgs.length) {
                res.push(restArgs[idx++]);
            } else {
                res.push(x);
            }
        }
        while (idx < restArgs.length) res.push(restArgs[idx++]);
        return fn(...res);
    };
}

Complexity

  • ⏰ Time complexity: O(n + m), where n is the length of args and m is the length of restArgs. We scan both arrays once.
  • 🧺 Space complexity: O(n + m), for the new argument array constructed before calling fn.