-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xjq
committed
Nov 16, 2022
1 parent
0ea1b42
commit 0ceca1c
Showing
11 changed files
with
181 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default function f(fn) { | ||
let _args = []; | ||
return (...args) => { | ||
let result = null; | ||
if (args.length) { | ||
_args = _args.concat(args); | ||
} else { | ||
result = fn(..._args); | ||
} | ||
return result; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## Curry | ||
|
||
实现一个柯里化函数 | ||
|
||
未传递参数时真正调用 | ||
|
||
用例 1: | ||
|
||
```js | ||
const add = (...args) => args.reduce((a, b) => a + b, 0); | ||
|
||
// 输入 [add, [1], [2], [3], null] | ||
|
||
// 执行 | ||
const curryAdd = currying(add); // true | ||
curryAdd(1); // null | ||
curryAdd(2); // null | ||
curryAdd(3); // null | ||
curryAdd(); // 6 | ||
|
||
// [true, null, null, null, 6] | ||
``` | ||
|
||
用例 2: | ||
|
||
```js | ||
const add = (...args) => args.reduce((a, b) => a + b, 0); | ||
|
||
// 输入 [add, [1, 2, 1], null, null, null] | ||
const curryAdd = currying(add); // true | ||
curryAdd(1, 2, 1); // null | ||
curryAdd(); // 4 | ||
curryAdd(); // 4 | ||
curryAdd(); // 4 | ||
|
||
// [true, null, 4, 4, 4] | ||
``` | ||
|
||
用例 3: | ||
|
||
```js | ||
const add = (...args) => args.reduce((a, b) => a + b, 0); | ||
|
||
// 输入 [add, [1, 2, 1], null, [1, 2], null] | ||
const curryAdd = currying(add); // true | ||
curryAdd(1, 2, 1); // null | ||
curryAdd(); // 4 | ||
curryAdd(1, 2); // null | ||
curryAdd(); // 7 | ||
|
||
// [true, null, 4, null, 7] | ||
``` | ||
|
||
用例 4: | ||
|
||
```js | ||
const multi = (...args) => args.reduce((a, b) => a * b, 1); | ||
|
||
// 输入 [multi, [100], null, [1, 2], null] | ||
const curryMulti = currying(multi); | ||
curryMulti(100); // null | ||
curryMulti(); // 100 | ||
curryMulti(1, 2); // null | ||
curryMulti(); // 200 | ||
// [true, null, 100, null, 200] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* 柯里化函数 | ||
* | ||
* @export | ||
* @param {Function} fn | ||
* @return {Function} | ||
*/ | ||
export default function currying(fn) { | ||
return fn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import assert from 'assert'; | ||
import f from './index.mjs'; | ||
|
||
const add = (...args) => args.reduce((a, b) => a + b, 0); | ||
const multi = (...args) => args.reduce((a, b) => a * b, 1); | ||
|
||
const cases = [ | ||
{ | ||
Input: [add, [1], [2], [3], null], | ||
Expected: [true, null, null, null, 6], | ||
Message: '加法函数柯里化一', | ||
}, | ||
{ | ||
Input: [add, [1, 2, 1], null, null, null], | ||
Expected: [true, null, 4, 4, 4], | ||
Message: '加法函数柯里化二', | ||
}, | ||
{ | ||
Input: [add, [1, 2, 1], null, [1, 2], null], | ||
Expected: [true, null, 4, null, 7], | ||
Message: '加法函数柯里化三', | ||
}, | ||
{ | ||
Input: [multi, [100], null, [1, 2], null], | ||
Expected: [true, null, 100, null, 200], | ||
Message: '乘法函数柯里化一', | ||
}, | ||
]; | ||
|
||
(function () { | ||
for (let i = 0; i < cases.length; i++) { | ||
const { Input, Expected, Message } = cases[i]; | ||
const output = []; | ||
try { | ||
let curry; | ||
Input.forEach((arg) => { | ||
let result; | ||
if (!curry) { | ||
curry = f(arg); | ||
result = !!curry; | ||
} else if (arg) { | ||
result = curry(...arg); | ||
} else { | ||
result = curry(); | ||
} | ||
output.push(result); | ||
}); | ||
|
||
assert.deepEqual(output, Expected); | ||
} catch (error) { | ||
console.log('用例 ' + String(i + 1) + ': ' + Message + ' 未通过'); | ||
if (error.code === 'ERR_ASSERTION') { | ||
console.log('Input:', Input); | ||
console.log('Expected:', JSON.stringify(error.expected)); | ||
console.log('Received:', JSON.stringify(output)); | ||
} else { | ||
console.log(error); | ||
} | ||
break; | ||
} | ||
console.log('用例 ' + String(i + 1) + ': ' + Message + ' 通过'); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
export default function f(arr) { | ||
/** | ||
* 排序函数 | ||
* | ||
* @export | ||
* @param {number[]} arr | ||
* @return {number[]} | ||
*/ | ||
export default function sort(arr) { | ||
return []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
/** | ||
* 数组去重 | ||
* | ||
* @export | ||
* @param {number[]} arr | ||
* @return {number[]} | ||
*/ | ||
export default function unique(arr) { | ||
return []; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.