mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
66 lines
2.6 KiB
HTML
66 lines
2.6 KiB
HTML
<p>Given a function <code>fn</code>, return a <strong>curried</strong> version of that function.</p>
|
|
|
|
<p>A <strong>curried</strong> function is a function that accepts fewer or an equal number of parameters as the original function and returns either another <strong>curried</strong> function or the same value the original function would have returned.</p>
|
|
|
|
<p>In practical terms, if you called the original function like <code>sum(1,2,3)</code>, you would call the <strong>curried</strong> version like <code>csum(1)(2)(3)<font face="sans-serif, Arial, Verdana, Trebuchet MS">, </font></code><code>csum(1)(2,3)</code>, <code>csum(1,2)(3)</code>, or <code>csum(1,2,3)</code>. All these methods of calling the <strong>curried</strong> function should return the same value as the original.</p>
|
|
|
|
<p> </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> </p>
|
|
<p><strong>Constraints:</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>function parameters explicitly defined</code></li>
|
|
</ul>
|