我正在查看是否可以撰寫一個階乘方法,如下所示:
class InlineMath {
constructor(x) {
this.x = x
}
minus(y) {
this.x -= y;
return this;
}
times(y) {
this.x *= y;
return this;
}
factorial(n) {
if (n == 1) {
return this;
} else {
this.x = this.times(this.factorial(this.minus(1)));
}
}
}
x = new InlineMath(2);
x.factorial().factorial();
console.log(x);
我知道這是一種完全非標準的方法,但只是看看這種方法是否可行(即,僅通過副作用)。
但到目前為止,我能弄清楚如何做到這一點的唯一方法就是像這樣卸載它:
class InlineMath {
constructor(x) {
this.x = x
}
minus(y) {
this.x -= y;
return this;
}
times(y) {
this.x *= y;
return this;
}
factorial() {
this.x = _factorial(this.x);
return this;
}
}
function _factorial(n) {
if (n==1) {
return n;
} else {
return n * _factorial(n-1);
}
}
x = new InlineMath(2);
x.factorial().factorial();
console.log(x);
uj5u.com熱心網友回復:
模塊
我建議math用普通函式撰寫你的模塊。您可以提供一個InlineMath類作為普通函式的薄包裝器。這使得撰寫普通函式和類更容易 -
// math.js
const minus = (a, b) => a - b
const plus = (a, b) => a b
const times = (a, b) => a * b
const factorial = a => a == 0 ? 1 : times(a, factorial(minus(a, 1)))
const math = a => new InlineMath(a)
class InlineMath {
constructor(t) { this.t = t }
factorial() { return math(factorial(this.t)) }
minus(x) { return math(minus(this.t, x)) }
plus(x) { return math(plus(this.t, x)) }
times(x) { return math(times(this.t, x)) }
toNumber() { return this.t }
}
export { math, minus, plus, times, factorial }
// main.js
import { math } from "./math.js"
console.log(math(3).factorial().factorial().toNumber())
console.log(math(1).plus(2).times(5).factorial().toNumber())
720
1307674368000
吃你的蛋糕,也吃
上述方法的一個低調的優點是我們的math模塊有一個雙介面。我們可以按照上面提出的面向物件的方式使用它,或者我們可以將它與函式式方法一起使用——
// main.js
import { plus, times, factorial } from "./math"
console.log(factorial(factorial(3)))
console.log(factorial(times(5,plus(2,1))))
720
1307674368000
低垂的果實
math如果可以支持非常大的數字,也許會很酷?
// math.js
const minus = (a, b) => BigInt(a) - BigInt(b)
const plus = (a, b) => BigInt(a) BigInt(b)
const times = (a, b) => BigInt(a) * BigInt(b)
const factorial = a => /* unchanged */
const math = a => /* unchanged */
class InlineMath {
/* ... */
toString() { return this.t.toString() }
}
export { math, minus, plus, times, factorial }
// main.js
import { math } from "./math.js"
console.log(math(5).factorial().factorial().toNumber())
5!!= 120!=
6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000
演示
運行下面的代碼片段以在您自己的瀏覽器中驗證結果 -
// math.js module
const minus = (a, b) => BigInt(a) - BigInt(b)
const plus = (a, b) => BigInt(a) BigInt(b)
const times = (a, b) => BigInt(a) * BigInt(b)
const factorial = a => a == 0 ? 1 : times(a, factorial(minus(a, 1)))
const math = a => new InlineMath(a)
class InlineMath {
constructor(t) { this.t = t }
factorial() { return math(factorial(this.t)) }
minus(x) { return math(minus(this.t, x)) }
plus(x) { return math(plus(this.t, x)) }
times(x) { return math(times(this.t, x)) }
toNumber() { return this.t.toString() }
}
// main.js
console.log(math(5).factorial().factorial().toNumber())
uj5u.com熱心網友回復:
這里有多個錯誤。
- 不要重新分配
this.x,保持你的實體不可變。 - 該
factorial方法不應該帶引數,它應該使用存盤在實體中的值 - 因此,
factorial需要在不同的實體上進行遞回呼叫,而不是給定引數 - 該
factorial方法必須始終回傳一個新實體,而不是像您當前的else分支一樣。
通過一些重命名:
class InlineNumber {
constructor(n) {
this.n = n
}
factorial() {
if (this.n == 1) {
// return 1
return this;
} else {
// return n * (n-1)!
return this.times(this.minus(new InlineNumber(1)).factorial());
}
}
minus(subtrahend) {
return new InlineNumber(this.n - subtrahend.n);
}
times(multiplicand) {
return new InlineNumber(this.n * multiplicand.n);
}
valueOf() {
return this.n;
}
}
const x = new InlineNumber(3);
console.log(x.factorial().factorial());
uj5u.com熱心網友回復:
在階乘公式n * (n-1)!中,您需要n兩次該值。使用可變數學物件,您需要克隆它以進行遞回呼叫:
class InlineMath {
constructor(x) {
this.x = x
}
clone() {
return new InlineMath(this.x);
}
minus(y) {
this.x -= y;
return this;
}
times(y) {
this.x *= y;
return this;
}
factorial() {
if (this.x > 1)
this.times(this.clone().minus(1).factorial().x);
return this;
}
}
x = new InlineMath(3);
x.factorial().factorial();
console.log(x);
請注意,撰寫公式(n-1)! * n并不容易:
this.minus(1).factorial().times(this.clone().x)
由于克隆發生得太晚,因此無法正常作業。我推薦我的另一個答案,它可以避免這種蠕蟲……
uj5u.com熱心網友回復:
我希望這只是一個概念驗證,并且您永遠不會打算使用這樣的代碼。但是對您的代碼進行簡單的更改將“通過副作用”實作這一點:
class InlineMath {
constructor(x) {
this.x = x
}
minus(y) {
this.x -= y;
return this;
}
times(y) {
this.x *= y;
return this;
}
factorial() {
if (this.x <= 1) {
this.x = 1;
} else {
this.x *= this.minus(1).factorial().x
}
return this;
}
}
const x = new InlineMath(3);
x.factorial().factorial();
console.log(x);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/417790.html
標籤:
上一篇:實作reduxcompose函式但得到RangeError
下一篇:遞回函式拼寫元素
