首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SafeMath类如何创建可链式计算?

SafeMath类如何创建可链式计算?
EN

Stack Overflow用户
提问于 2021-07-31 22:52:43
回答 3查看 221关注 0票数 0

我有一个困难的时间找到关键字搜索这个在线。

我创建了一个带有安全数学函数的类。每个函数都有两个参数,在被断言计算后,它返回结果。

示例:

代码语言:javascript
复制
class SafeMath {

  static add(x: number, y: number) {
    let z: number = x + y;
    assert(z >= x, 'ds-math-add-overflow');
    return z;
  }

  static sub(x: number, y: number) {
    let z: number = x - y;
    assert(z <= x, 'ds-math-sub-underflow');
    return z;
  }

  static mul(x: number, y: number) {
    let z: number = x * y;
    assert(y == 0 || z / y == x, 'ds-math-mul-overflow');
    return z;
  }

  static div(x: number, y: number) {
    let z: number = x / y;
    assert(x > 0 || y > 0, 'ds-math-div-by-zero');
    return z;
  }

}

console.log(SafeMath.add(2,2)); // 4
console.log(SafeMath.sub(2,2)); // 0
console.log(SafeMath.mul(2,2)); // 4
console.log(SafeMath.div(2,2)); // 1

我的目标是让这些功能像这样工作,例如:

代码语言:javascript
复制
let balance0: number = 1;
let balance1: number = 1;

let amount0In: number = 10;
let amount1In: number = 10;

let balance0Adjusted: number = balance0.mul(1000).sub(amount0In.mul(3));
let balance1Adjusted: number = balance1.mul(1000).sub(amount1In.mul(3));

...the函数将接受y,并使用前面的数字作为x

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-08-01 14:18:15

您可以修改Number.prototype以添加函数,以便链接这些操作。使用string属性键这样做通常被认为是一种糟糕的做法(参见为什么扩展本机对象是一种糟糕的实践?)。您可以使用唯一的符号属性键而不是字符串属性键来避免名称冲突等。

下面是一个示例模块,它使用唯一的符号安全地“扩展”Number.prototype的乘法函数,并将新的函数签名添加到TypeScript Number接口中:

mul.ts

代码语言:javascript
复制
const mul = Symbol("multiply");

function value(this: number, n: number) {
  return this * n;
}

declare global {
  interface Number {
    [mul]: typeof value;
  }
}

Object.defineProperty(Number.prototype, mul, { value });

export default mul;

在为减法、加法、除法等定义了像上面这样的模块之后,您可以导入这些模块并使用它们导出的唯一符号来进行链操作:

代码语言:javascript
复制
import mul from "./mul.ts";
import sub from "./sub.ts";

const balance = 1;
const amountIn = 10;
const balanceAdjusted = balance[mul](1000)[sub](amountIn[mul](3));
console.log(balanceAdjusted);
代码语言:javascript
复制
970

将这些数学操作链接起来的一个准确之处是,每当您处理空值时,您都可以将它们与可选链式算子结合起来,这些值有时会派上用场。

不需要使用符号也可以这样做,但是对于可能为JavaScript定义自己的Number方法的未来版本mul,这是不安全的,等等:

mul.ts

代码语言:javascript
复制
function value(this: number, n: number) {
  return this * n;
}

declare global {
  interface Number {
    mul: typeof value;
  }
}

Object.defineProperty(Number.prototype, "mul", { value });

export {}; // you have to import or export something to make it a module
代码语言:javascript
复制
import "./mul.ts";
import "./sub.ts";

const balance = 1;
const amountIn = 10;
const balanceAdjusted = balance.mul(1000).sub(amountIn.mul(3));
console.log(balanceAdjusted);
代码语言:javascript
复制
970

单独导入所有这些模块可能不太方便,因此您还可以创建一个模块来组合所有其他模块:

math.ts

代码语言:javascript
复制
export { default as mul } from "./mul.ts";
export { default as sub } from "./sub.ts";
/* and so forth */

然后您可以导入它并选择要使用的:

代码语言:javascript
复制
import { mul, sub } from "./math.ts";

const balance = 1;
const amountIn = 10;
const balanceAdjusted = balance[mul](1000)[sub](amountIn[mul](3));
console.log(balanceAdjusted);
票数 1
EN

Stack Overflow用户

发布于 2021-07-31 23:10:15

您可以为此做一些包装:

代码语言:javascript
复制
if (!Number.prototype.mul)  // check that the mul method does not already exist 
  {
  Number.prototype.mul = function(n){ return this * n }
  }
  
if (!Number.prototype.add)
  {
  Number.prototype.add = function(n){ return this + n }
  }
  
  
let val = 5
let doubleValPlus500 = val.mul(2).add(500)

console.log( doubleValPlus500 )

票数 2
EN

Stack Overflow用户

发布于 2021-08-01 00:12:05

基于Number.prototype的实例

代码语言:javascript
复制
import { assert } from "https://deno.land/std@0.102.0/testing/asserts.ts";

declare global {

  /*
    Augument global Number.prototype with the following custom functions

    Warning - While this may look like a clean approach, it is considered 
    unsafe due to javascript possibly choosing to natively implement these
    exact function names in the near future. To avoid this, choose unique
    function names.

  */
  interface Number {
    add(n: number): number;
    sub(n: number): number;
    mul(n: number): number;
    div(n: number): number;
    pow(n: number): number;
    sqrt(): number;
    print(): number;
  }
}

Number.prototype.add = function(this:number, n:number) {

  assert((this + n) >= this, 'ds-math-add-overflow');
  return this + n;
}

Number.prototype.sub = function(this:number, n:number) {

  assert((this - n) <= this, 'ds-math-sub-underflow');
  return this - n;
}

Number.prototype.mul = function(this:number, n:number) {

  assert(n == 0 || (this * n) / n == this, 'ds-math-mul-overflow');
  return this * n;
}

Number.prototype.div = function(this:number, n:number) {

  assert(this > 0 || n > 0, 'ds-math-div-by-zero');
  return this / n;
}

Number.prototype.pow = function(this:number, n:number) {

  assert(this > 0 && n >= 2, 'ds-math-exp-to-zero');
  return this ** n;
}

// babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
Number.prototype.sqrt = function(this:number) {

  assert(this > 0, 'ds-math-sqrt-of-zero');

  let x: number = 0;
  let y: number = this;
  let z: number = 0;

  if (y > 3) {
    z = y;
    x = y / 2 + 1;
    while (x < z) {
      z = x;
      x = (y / x + x) / 2;
    }
  } else if (y != 0) {
    z = 1;
  }
  return z;
}

Number.prototype.print = function(this:number) {

  console.log('=', this);
  return this;
}

// Here it is in action:

let balance = 0;

balance.add(10).print().sub(1).print().mul(2).print().div(3).print().pow(4).print().sqrt().print();

输出:

代码语言:javascript
复制
= 10
= 9
= 18
= 6
= 1296
= 36

基于类的示例

代码语言:javascript
复制
import { assert } from "https://deno.land/std@0.102.0/testing/asserts.ts";

class SafeMath {
  private n: number;

  constructor(start: number = 0) {
    this.n = start;
  }

  public add(y: number) {

    assert(this.n + y >= this.n, 'ds-math-add-overflow');

    let z: number = this.n + y;

    this.n = this.n + y;
    return this;
  }

  public sub(y: number) {

    assert(this.n - y <= this.n, 'ds-math-sub-underflow');

    let z: number = this.n - y;

    this.n = this.n - y;
    return this;
  }

  public mul(y: number) {

    assert(y == 0 || (this.n * y) / y == this.n, 'ds-math-mul-overflow');

    let z: number = this.n * y;

    this.n = this.n * y;
    return this;
  }

  public div(y: number) {

    assert(this.n > 0 || y > 0, 'ds-math-div-by-zero');

    let z: number = this.n / y;

    this.n = this.n / y;
    return this;
  }

  public pow(y: number) {

    assert(this.n > 0 && y >= 2, 'ds-math-exp-to-zero');

    let z: number = this.n ** y;
    this.n = this.n ** y;
    return this;
  }

  // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
  public sqrt() {

    assert(this.n > 0, 'ds-math-sqrt-of-zero');

    let x: number = 0;
    let y: number = this.n;
    let z: number = 0;

    if (y > 3) {
      z = y;
      this.n = z;
      x = y / 2 + 1;
      while (x < z) {
        z = x;
        this.n = z
        x = (y / x + x) / 2;
      }
    } else if (y != 0) {
      z = 1;
      this.n = z
    }
    return this;
  }

  public print() {
    console.log('=', this.n);
    return this;
  }

}

// Here it is in action:

new SafeMath(0).add(10).print().sub(1).print().mul(2).print().div(3).print().pow(4).print().sqrt().print();

输出:

代码语言:javascript
复制
= 10
= 9
= 18
= 6
= 1296
= 36
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68606326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档