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).

1  
sclass VarBasedArithmeticEngine {
2  
  MapSO vars = syncTreeMap();
3  
  
4  
  sclass NotRunYet {}
5  
  static new NotRunYet notRunYet;
6  
  
7  
  record SetVar(S var, O value) is Runnable {
8  
    run { setVar(var, value); }
9  
  }
10  
  
11  
  // SINGLE-OPS (one input variable, one output variable)
12  
  
13  
  macro ctors {  
14  
    *() {}
15  
    *(S *inVar, S *outVar) {}
16  
  }
17  
  
18  
  abstract noeq record SingleOp(S inVar, S outVar) is Runnable {
19  
    transient O value = notRunYet;
20  
    
21  
    abstract O perform(O arg);
22  
    
23  
    run { get(); }
24  
    
25  
    O get() {
26  
      value = perform(getVar(inVar));
27  
      setVar(outVar, value);
28  
      ret value;
29  
    }
30  
  }
31  
  
32  
  class Sqrt > SingleOp { ctors
33  
    Double perform(O arg) {
34  
      ret sqrt(toDouble(arg));
35  
    }
36  
  }
37  
  
38  
  // MULTI-OPS (multiple input variables, one output variable)
39  
  
40  
  abstract noeq record MultiOp(LS inVars, S outVar) is Runnable {
41  
    transient O value = notRunYet;
42  
    
43  
    abstract O perform(L values);
44  
    
45  
    *(S... vars) {
46  
      outVar = last(vars);
47  
      inVars = asListMinusLast(vars);
48  
    }
49  
    
50  
    run { get(); }
51  
    
52  
    O get() {
53  
      value = perform(map getVar(inVars);
54  
      setVar(outVar, value);
55  
      ret value;
56  
    }
57  
  }
58  
59  
  macro ctors {  
60  
    *() {}
61  
    *(LS *inVars, S *outVar) {}
62  
    *(S... vars) { super(vars); }
63  
  }
64  
  
65  
  // add doubles
66  
  class DAdd > MultiOp { ctors
67  
    Double perform(L values) {
68  
      ret doubleSum(allToDouble(values));
69  
    }
70  
  }
71  
  
72  
  // multiply doubles
73  
  persistent class DMul > MultiOp { ctors
74  
    Double perform(L values) {
75  
      ret doubleProduct(allToDouble(values));
76  
    }
77  
  }
78  
  
79  
  // subtract doubles (a-b-c...)
80  
  class DMinus > MultiOp { ctors
81  
    Double perform(L values) {
82  
      ret doubleMultiMinus(allToDouble(values));
83  
    }
84  
  }
85  
86  
  // division (a/b/c...) with division by zero giving 0
87  
  class DDiv > MultiOp { ctors
88  
    Double perform(L values) {
89  
      ret doubleMultiRatio(allToDouble(values));
90  
    }
91  
  }
92  
  
93  
  O getVar aka get(S var) { ret vars.get(var); }
94  
  void setVar aka set(S var, O value) { if (var != null) vars.put(var, value); }
95  
  
96  
  *(O... _) {
97  
    addParamsToMap_inPlace(vars, _);
98  
  }
99  
}

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: 137 / 271
Version history: 12 change(s)
Referenced in: [show references]