tc39 / proposal-eventual-send

TC39 Eventual Send proposal
44 stars 6 forks source link

A handy shim to acompany eventual-sends #17

Open zarutian opened 4 years ago

zarutian commented 4 years ago

Here is a handy shim/polyfill/whatchacallit that will be usefull in conjunction with eventual-send. Allows for promise pipelined logic and arithmatic for those situations that warrant it.

    Boolean.prototype.pick = function (conseq, altern) {
      return (this.valueOf() ? conseq : altern);
    }
    Boolean.prototype.not = function () {
      return !(this.valueOf());
    }
    Boolean.prototype.and = function (other) {
      return (this.valueOf() && Boolean(other));
    }
    Boolean.prototype.or = function (other) {
      return (this.valueOf() || Boolean(other));
    }
    BigInt.prototype.add = function (other) {
      return (this.valueOf()) + BigInt(other);
    }
    BigInt.prototype.subtract = function (other) {
      return (this.valueOf()) - BigInt(other);
    }
    BigInt.prototype.multiply = function (other) {
       return (this.valueOf()) * BigInt(other);
    }
    BigInt.prototype.divide = function (other) {
      return (this.valueOf()) / BigInt(other); // /
    }
    BigInt.prototype.modulo = function (other) {
      return (this.valueOf()) % BigInt(other);
    }
    BigInt.prototype.exponate = function (other) {
      return (this.valueOf()) ** BigInt(other);
    }
    BigInt.prototype.negate = function () {
      return -(this.valueOf());
    }
    BigInt.prototype.bitwiseNot = function () {
      return ~(this.valueOf());
    }
    BigInt.prototype.bitwiseAnd = function (other) {
      return (this.valueOf()) & BigInt(other);
    }
    BigInt.prototype.bitwiseOr = function (other) {
      return (this.valueOf()) | BigInt(other);
    }
    BigInt.prototype.bitwiseXor = function (other) {
      return (this.valueOf()) ^ BigInt(other);
    }
    BigInt.prototype.toBoolArray = function (nrOfBits = 32) {
      var v = this.valueOf();
      const r = [];
      while (nrOfBits > 0) {
        r.push(((v & 0x1) == 0x1));
        v = v >> 1;
        nrOfBits--;
      }
      return r;
    }
    Number.prototype.toBoolArray = BigInt.prototype.toBoolArray;
    Number.prototype.toBigInt = function () {
      return BigInt(this.valueOf());
    }
    BigInt.fromBoolArray = (arr) => {
      var n = BigInt(0);
      while (arr.length > 0) {
        n = n << 1;
        n = n + (arr.pop() ? BigInt(1) : BigInt(0));
      }
      return n;
    }
    BigInt.prototype.isZero = function () {
      return (this.valueOf() == 0n);
    }
    BigInt.prototype.isGreaterThan = function (other) {
      return (this.valueOf() > BigInt(other));
    }
    BigInt.prototype.isLessThan = function (other) {
      return (this.valueOf() < BigInt(other));
    }
    BigInt.prototype.isEqual = function (other) {
      return (this.valueOf() == BigInt(other));
    }
zarutian commented 4 years ago

Two additional functions that might be handy:

  const someAwait = async (args, which) => {
    const awaited = await Promise.all(which.map((w, i) => (w ? args[i] : null)));
    return which.map((w, i) => ( w ? awaited[i] : args[i]));
  }
  const someArgsAwait_method = async (target, verb, args, which) => {
    return HandledPromise.applyMethod(target, verb, await someAwait(args, which) );
  }
  const someArgsAwait_function = async (target, args, which) => {
    return HandledPromise.applyFunction(target, await someAwait(args, which) );
  }

Specially, if these are at remote vats/sites, for enabling lots of pipelining where some of the arguments are unresolved at time of eventual send. I am up for bikeshedding better names for these functions though.