Skip to content

Commit 1a665c5

Browse files

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Step 2. Add the dependency
4949

5050
<summary>展开查看</summary>
5151

52+
https://leetcode.cn/problems/array-reduce-transformation/
53+
54+
https://leetcode.cn/problems/curry
55+
5256
https://leetcode.cn/problems/memoize-ii/
5357

5458
https://leetcode.cn/problems/cache-with-time-limit/

array-reduce-transformation/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type Fn = (accum: number, curr: number) => number;
2+
3+
function reduce(nums: number[], fn: Fn, init: number): number {
4+
for (const v of nums) {
5+
init = fn(init, v);
6+
}
7+
return init;
8+
}
9+
export { reduce as default };
10+
export type { Fn };

curry/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// deno-lint-ignore ban-types
2+
function curry(fn: Function): Function {
3+
const length = fn.length;
4+
return function curried(...args: any[]) {
5+
if (args.length === length) return fn(...args);
6+
if (args.length < length) {
7+
return curry(fn.bind(null, ...args));
8+
}
9+
throw new Error("arguments length mismatch");
10+
};
11+
}
12+
13+
/**
14+
* function sum(a, b) { return a + b; }
15+
* const csum = curry(sum);
16+
* csum(1)(2) // 3
17+
*/
18+
19+
export default curry;

0 commit comments

Comments
 (0)