Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

99
LINES

< > BotCompany Repo | #1032649 // VarBasedArithmeticEngine

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4596L/26K).

sclass VarBasedArithmeticEngine {
  MapSO vars = syncTreeMap();
  
  sclass NotRunYet {}
  static new NotRunYet notRunYet;
  
  record SetVar(S var, O value) is Runnable {
    run { setVar(var, value); }
  }
  
  // SINGLE-OPS (one input variable, one output variable)
  
  macro ctors {  
    *() {}
    *(S *inVar, S *outVar) {}
  }
  
  abstract noeq record SingleOp(S inVar, S outVar) is Runnable {
    transient O value = notRunYet;
    
    abstract O perform(O arg);
    
    run { get(); }
    
    O get() {
      value = perform(getVar(inVar));
      setVar(outVar, value);
      ret value;
    }
  }
  
  class Sqrt > SingleOp { ctors
    Double perform(O arg) {
      ret sqrt(toDouble(arg));
    }
  }
  
  // MULTI-OPS (multiple input variables, one output variable)
  
  abstract noeq record MultiOp(LS inVars, S outVar) is Runnable {
    transient O value = notRunYet;
    
    abstract O perform(L values);
    
    *(S... vars) {
      outVar = last(vars);
      inVars = asListMinusLast(vars);
    }
    
    run { get(); }
    
    O get() {
      value = perform(map getVar(inVars);
      setVar(outVar, value);
      ret value;
    }
  }

  macro ctors {  
    *() {}
    *(LS *inVars, S *outVar) {}
    *(S... vars) { super(vars); }
  }
  
  // add doubles
  class DAdd > MultiOp { ctors
    Double perform(L values) {
      ret doubleSum(allToDouble(values));
    }
  }
  
  // multiply doubles
  persistent class DMul > MultiOp { ctors
    Double perform(L values) {
      ret doubleProduct(allToDouble(values));
    }
  }
  
  // subtract doubles (a-b-c...)
  class DMinus > MultiOp { ctors
    Double perform(L values) {
      ret doubleMultiMinus(allToDouble(values));
    }
  }

  // division (a/b/c...) with division by zero giving 0
  class DDiv > MultiOp { ctors
    Double perform(L values) {
      ret doubleMultiRatio(allToDouble(values));
    }
  }
  
  O getVar aka get(S var) { ret vars.get(var); }
  void setVar aka set(S var, O value) { if (var != null) vars.put(var, value); }
  
  *(O... _) {
    addParamsToMap_inPlace(vars, _);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032649
Snippet name: VarBasedArithmeticEngine
Eternal ID of this version: #1032649/13
Text MD5: 0708e4db737b302ca58e932ea07c163c
Transpilation MD5: b83a6bafd657ba9c806d4d7c0600953f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-30 05:25:20
Source code size: 2175 bytes / 99 lines
Pitched / IR pitched: No / No
Views / Downloads: 249 / 375
Version history: 12 change(s)
Referenced in: #1032804 - OpsOnVars [dev.]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)