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

49
LINES

< > BotCompany Repo | #1015715 // Deferred Computation [Include]

JavaX fragment (include)

1  
svoid allowDeferredComputation(Runnable r) {
2  
  try {
3  
    callF(r);
4  
  } catch (RuntimeException e) {
5  
    Throwable e2 = innerException(e);
6  
    if (!e2 instanceof DeferredComputationException) throw rethrow(e);
7  
  }
8  
}
9  
10  
// f1: func -> A, f2 : func(A) -> B, returns: B
11  
static O chainDeferrableComputations(O f1, fO f2) {
12  
  O result;
13  
  try {
14  
    result = callF(f1);
15  
  } catch (RuntimeException e) {
16  
    Throwable e2 = innerException(e);
17  
    if (!e2 instanceof DeferredComputationException) throw rethrow(e);
18  
    final DeferredComputation f = e2/DeferredComputationException.future;
19  
    
20  
    final new DeferredComputation future2;
21  
    f.setContinuation(r {
22  
      try {
23  
        future2.setValue(callF(f2, f.getValue()));
24  
      } catch e {
25  
        future2.setError(e);
26  
      }
27  
    });
28  
    throw DeferredComputationException(future2);
29  
  }
30  
  ret callF(f2, result);
31  
}
32  
33  
static <A> A waitThenCompute(int delay, final F0<A> f) {
34  
  final new DeferredComputation dc;
35  
  doLater(delay, r {
36  
    try {
37  
      dc.setValue(callF(f));
38  
    } catch e {
39  
      dc.setError(e);
40  
    }
41  
  });
42  
  throw DeferredComputationException(dc);
43  
}
44  
45  
sclass DeferredComputationException extends QuickException {
46  
  DeferredComputation future;
47  
48  
  *(DeferredComputation *future) {}
49  
}

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: #1015715
Snippet name: Deferred Computation [Include]
Eternal ID of this version: #1015715/3
Text MD5: b933e46917dbe1ceedbbb0f2b78ea1a1
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-24 17:49:33
Source code size: 1234 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 289 / 337
Version history: 2 change(s)
Referenced in: [show references]