1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (English)/柯里化(English) [curry].html

66 lines
2.6 KiB
HTML
Raw Permalink Normal View History

2023-04-23 22:41:08 +08:00
<p>Given a function&nbsp;<code>fn</code>,&nbsp;return&nbsp;a&nbsp;<strong>curried</strong>&nbsp;version of that function.</p>
<p>A&nbsp;<strong>curried</strong>&nbsp;function is a function that accepts fewer or an equal number of&nbsp;parameters as the original function and returns either another&nbsp;<strong>curried</strong>&nbsp;function or the same value the original function would have returned.</p>
<p>In practical terms, if you called the original function like&nbsp;<code>sum(1,2,3)</code>, you would call the&nbsp;<strong>curried</strong>&nbsp;version like <code>csum(1)(2)(3)<font face="sans-serif, Arial, Verdana, Trebuchet MS">,&nbsp;</font></code><code>csum(1)(2,3)</code>,&nbsp;<code>csum(1,2)(3)</code>, or&nbsp;<code>csum(1,2,3)</code>. All these methods of calling the <strong>curried</strong> function&nbsp;should return the same value as the original.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong>
fn = function sum(a, b, c) { return a + b + c; }
inputs = [[1],[2],[3]]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
The code being executed is:
const curriedSum = curry(fn);
curriedSum(1)(2)(3) === 6;
curriedSum(1)(2)(3) should return the same value as sum(1, 2, 3).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong>
fn = function sum(a, b, c) { return a + b + c; }
inputs = [[1,2],[3]]]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
curriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong>
fn = function sum(a, b, c) { return a + b + c; }
inputs = [[],[],[1,2,3]]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
You should be able to pass the parameters in any way, including all at once or none at all.
curriedSum()()(1, 2, 3) should return the same value as sum(1, 2, 3).
</pre>
<p><strong class="example">Example 4:</strong></p>
<pre>
<strong>Input:</strong>
fn = function life() { return 42; }
inputs = [[]]
<strong>Output:</strong> 42
<strong>Explanation:</strong>
currying a function that accepts zero parameters should effectively do nothing.
curriedLife() === 42
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= inputs.length &lt;= 1000</code></li>
<li><code>0 &lt;= inputs[i][j] &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= fn.length &lt;= 1000</code></li>
<li><code>inputs.flat().length == fn.length</code></li>
<li><code>function parameters explicitly defined</code></li>
</ul>