mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
68 lines
2.3 KiB
HTML
68 lines
2.3 KiB
HTML
|
<p>请你编写一个函数,它接收一个其他的函数,并返回该函数的 <strong>柯里化 </strong>后的形式。</p>
|
|||
|
|
|||
|
<p><strong>柯里化 </strong>函数的定义是接受与原函数相同数量或更少数量的参数,并返回另一个 <strong>柯里化</strong> 后的函数或与原函数相同的值。</p>
|
|||
|
|
|||
|
<p>实际上,当你调用原函数,如 <code>sum(1,2,3)</code> 时,它将调用 <strong>柯里化</strong> 函数的某个形式,如 <code>csum(1)(2)(3)</code>, <code>csum(1)(2,3)</code>, <code>csum(1,2)(3)</code>,或 <code>csum(1,2,3)</code> 。所有调用 <strong>柯里化</strong> 函数的方法都应该返回与原始函数相同的值。</p>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>示例 1:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<b>输入:</b>
|
|||
|
fn = function sum(a, b, c) { return a + b + c; }
|
|||
|
inputs = [[1],[2],[3]]
|
|||
|
<b>输出:</b>6
|
|||
|
<strong>解释:</strong>
|
|||
|
执行的代码是:
|
|||
|
const curriedSum = curry(fn);
|
|||
|
curriedSum(1)(2)(3) === 6;
|
|||
|
curriedSum(1)(2)(3) 应该返回像原函数 sum(1, 2, 3) 一样的值。
|
|||
|
</pre>
|
|||
|
|
|||
|
<p><strong>示例 2:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>
|
|||
|
fn = function sum(a, b, c) { return a + b + c; }
|
|||
|
inputs = [[1,2],[3]]]
|
|||
|
<b>输出:</b>6
|
|||
|
<strong>解释:</strong>
|
|||
|
curriedSum(1, 2)(3) 应该返回像原函数 sum(1, 2, 3) 一样的值。</pre>
|
|||
|
|
|||
|
<p><strong>示例 3:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>
|
|||
|
fn = function sum(a, b, c) { return a + b + c; }
|
|||
|
inputs = [[],[],[1,2,3]]
|
|||
|
<b>输出:</b>6
|
|||
|
<strong>解释:</strong>
|
|||
|
你应该能够以任何方式传递参数,包括一次性传递所有参数或完全不传递参数。
|
|||
|
curriedSum()()(1, 2, 3) 应该返回像原函数 sum(1, 2, 3) 一样的值。
|
|||
|
</pre>
|
|||
|
|
|||
|
<p><strong>示例 4:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>
|
|||
|
fn = function life() { return 42; }
|
|||
|
inputs = [[]]
|
|||
|
<b>输出:</b>42
|
|||
|
<strong>解释:</strong>
|
|||
|
柯里化一个没有接收参数,没做有效操作的函数。
|
|||
|
curriedLife() === 42
|
|||
|
</pre>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>提示:</strong></p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><code>1 <= inputs.length <= 1000</code></li>
|
|||
|
<li><code>0 <= inputs[i][j] <= 10<sup>5</sup></code></li>
|
|||
|
<li><code>0 <= fn.length <= 1000</code></li>
|
|||
|
<li><code>inputs.flat().length == fn.length</code></li>
|
|||
|
<li><code>函数参数需要被显式定义</code></li>
|
|||
|
</ul>
|