我正在使用React / Redux制作数独Web应用程序。但是我在键入时遇到了一些问题。
当前代码:
// typedef
type Tuple9<T> = [T, T, T, T, T, T, T, T, T];
export type Board = Tuple9<Tuple9<number>>;
// code using board type, I want to fix getEmptyBoard() to more programmatic.
const getEmptyBoard: (() => Board) = () => {
return [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
];
};
我想解决getEmptyBoard()
更多程序问题。
因为9
我会做你所做的。
否则,您将遵循古老的函数式编程的说法:If its pure on the outside, it doesn't matter if its impure on the inside
并战略性地使用Tuple9
类型断言:
type Tuple9<T> = [T, T, T, T, T, T, T, T, T];
function make9<T>(what: T): Tuple9<T> {
return new Array(9).fill(what) as Tuple9<T>;
}
export type Board = Tuple9<Tuple9<number>>;
function makeBoard(): Board {
return make9(make9(0));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句