跳至主要內容

2797. 带有占位符的部分函数 🔒


2797. 带有占位符的部分函数 🔒

🟢   🔗 力扣open in new window LeetCodeopen in new window

题目

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:

  • 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

题目大意

给定函数 fn 和数组 args,返回一个函数 partialFn

args 中的占位符 "_" 需要用 restArgs 中索引从 0 开始的值替换。 restArgs 中剩余的值则添加到 args 的末尾。

partialFn 应该返回 fn 的结果。fn 应该使用修改后的 args 的元素作为单独的参数调用。

示例 1:

输入: fn = (...args) => args, args = [2,4,6], restArgs = [8,10]

输出:[2,4,6,8,10]

解释:

const partialFn = partial(fn, args);
const result = partialFn(...restArgs);
console.log(result); // [2,4,6,8,10]

args 中没有占位符 "_",因此 restArgs 只是添加到 args 的末尾。然后将 args 的元素作为单独的参数传递给 fn,fn 返回传递的参数作为数组。

示例 2:

输入: fn = (...args) => args, args = [1,2,"",4,"",6], restArgs = [3,5]

输出:[1,2,3,4,5,6]

解释:

const partialFn = partial(fn, args);
const result = partialFn(...restArgs);
console.log(result); // [1,2,3,4,5,6]

占位符 "_" 被 restArgs 中的值替换。然后将 args 的元素作为单独的参数传递给 fn,fn 返回传递的参数作为数组。

示例 3:

输入: fn = (a, b, c) => b + a - c, args = ["_", 5], restArgs = [5, 20]

输出: -10

解释:

const partialFn = partial(fn, args);
const result = partialFn(...restArgs);
console.log(result); // -10

占位符 "_" 被替换为 5,并将 20 添加到 args 的末尾。然后将 args 的元素作为单独的参数传递给 fn,fn 返回 -10(5 + 5 - 20)。

提示:

  • fn 是一个函数
  • argsrestArgs 都是有效的 JSON 数组
  • 1 <= args.length <= 5 * 10^4
  • 1 <= restArgs.length <= 5 * 10^4
  • 0 <= number of placeholders <= restArgs.length

解题思路

为了实现这个 partialFn,可以按照以下步骤处理 args 数组中的占位符 "_",然后将 restArgs 中的元素替换这些占位符并拼接在一起:

  1. 定义 partialFn 函数,该函数接受 restArgs 参数。
  2. 遍历 args 数组,如果遇到 "_",将其替换为 restArgs 中相应的值,并在 restArgs 中移除该值。
  3. 将剩余的 restArgs 添加到 args 的末尾,以完成参数列表。
  4. 使用展开运算符将修改后的 args 作为参数调用 fn

代码

/**
 * @param {Function} fn
 * @param {Array} args
 * @return {Function}
 */
var partial = function (fn, args) {
	return function (...restArgs) {
		let filledArgs = args.map((arg) => (arg === '_' ? restArgs.shift() : arg));
		return fn(...filledArgs, ...restArgs);
	};
};