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

58
LINES

< > BotCompany Repo | #1012585 // Reified Calculation v2 [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (1845L/13K).

1  
!7
2  
3  
sclass Calculation {
4  
  O f;
5  
  Calculation deferredTo;
6  
  OKOrError result;
7  
  
8  
  *() {}
9  
  *(O *f) {}
10  
  
11  
  void calc() {
12  
    if (done()) ret;
13  
    try {
14  
      result = OKOrError_ok(callF(f));
15  
    } catch e {
16  
      Throwable inner = getInnerException(e);
17  
      if (inner instanceof DeferCalculation)
18  
        deferredTo = inner/DeferCalculation.calculation();
19  
      else
20  
        result = OKOrError_error(e);
21  
    }
22  
  }
23  
  
24  
  bool done() { ret result != null || deferredTo != null; }
25  
  
26  
  toString {
27  
    S s = "Calculation of " + f + ".";
28  
    if (deferredTo != null) s += " Deferred to: " + deferredTo;
29  
    else if (result != null) s += " " + result;
30  
    else s += " Not run";
31  
    ret s;
32  
  }
33  
  
34  
  Calculation resolve() {
35  
    Calculation c = this;
36  
    while (c.deferredTo != null) c = c.deferredTo;
37  
    ret c;
38  
  }
39  
}
40  
41  
p {
42  
  Calculation c = new Calculation(f now);
43  
  print(c);
44  
  c.calc();
45  
  print(c);
46  
  
47  
  c = new Calculation(f fail);
48  
  c.calc();
49  
  print(c);
50  
  
51  
  c = new Calculation(r { throw DeferCalculation(Calculation(f now)) });
52  
  print(c);
53  
  c.calc();
54  
  print(c);
55  
  c = c.resolve();
56  
  c.calc();
57  
  print(c);
58  
}

Author comment

Began life as a copy of #1012583

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1012585
Snippet name: Reified Calculation v2 [OK]
Eternal ID of this version: #1012585/3
Text MD5: 7f158e8e503df1527bed136d1d8f36cd
Transpilation MD5: 720a6987f121845f022041a94aa3fcff
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-07 14:37:27
Source code size: 1164 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 506 / 1232
Version history: 2 change(s)
Referenced in: [show references]