-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2632-Curry.js
More file actions
90 lines (79 loc) · 2.77 KB
/
2632-Curry.js
File metadata and controls
90 lines (79 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 2632. Curry
// Given a function fn, return a curried version of that function.
// A curried function is a function that accepts fewer or an equal number of parameters as the original function and returns either another curried function or the same value the original function would have returned.
// In practical terms, if you called the original function like sum(1,2,3), you would call the curried version like csum(1)(2)(3), csum(1)(2,3), csum(1,2)(3), or csum(1,2,3).
// All these methods of calling the curried function should return the same value as the original.
// Example 1:
// Input:
// fn = function sum(a, b, c) { return a + b + c; }
// inputs = [[1],[2],[3]]
// Output: 6
// Explanation:
// 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).
// Example 2:
// Input:
// fn = function sum(a, b, c) { return a + b + c; }
// inputs = [[1,2],[3]]
// Output: 6
// Explanation:
// curriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).
// Example 3:
// Input:
// fn = function sum(a, b, c) { return a + b + c; }
// inputs = [[],[],[1,2,3]]
// Output: 6
// Explanation:
// 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).
// Example 4:
// Input:
// fn = function life() { return 42; }
// inputs = [[]]
// Output: 42
// Explanation:
// currying a function that accepts zero parameters should effectively do nothing.
// curriedLife() === 42
// Constraints:
// 1 <= inputs.length <= 1000
// 0 <= inputs[i][j] <= 10^5
// 0 <= fn.length <= 1000
// inputs.flat().length == fn.length
// function parameters explicitly defined
// If fn.length > 0 then the last array in inputs is not empty
// If fn.length === 0 then inputs.length === 1
/**
* @param {Function} fn
* @return {Function}
*/
var curry = function(fn) {
return function curried(...args) {
// 参数够了就返回结果
if(args.length >= fn.length) {
return fn(...args);
}
// 参数不够返回,接收剩余参数的函数 (...args, ...nextArgs) 是关键
return (...nextArgs) => curried(...args, ...nextArgs);
}
};
/**
* function sum(a, b) { return a + b; }
* const csum = curry(sum);
* csum(1)(2) // 3
*/
// Example 1:
let fn = function sum(a, b, c) { return a + b + c; }
const curriedSum = curry(fn);
console.log(fn);
console.log(curriedSum(1)(2)(3)); // 6
// Example 2:
console.log(curriedSum(1, 2)(3)); // 6
// Example 3:
console.log(curriedSum()()(1, 2, 3) ); // 6
// Example 4:
fn = function life() { return 42; }
const curriedLife = curry(fn);
console.log(fn);
console.log(curriedLife()); // 42