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

108
LINES

< > BotCompany Repo | #1015714 // Deferred Computation Spike

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

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (7642L/51K).

1  
!7
2  
3  
p-experiment {
4  
  allowDeferredComputation(r {
5  
    chainDeferrableComputations(f serveLater, fg(f print, f reversedString));
6  
  });
7  
  allowDeferredComputation(r {
8  
    chainDeferrableComputations(f serveNow, fg(f print, f reversedString));
9  
  });
10  
}
11  
12  
sS serveNow() {
13  
  ret "Served now";
14  
}
15  
16  
sS serveLater() {
17  
  ret waitThenCompute(1000, func -> S { "Served late" });
18  
}
19  
20  
svoid allowDeferredComputation(Runnable r) {
21  
  try {
22  
    callF(r);
23  
  } catch (RuntimeException e) {
24  
    Throwable e2 = innerException(e);
25  
    if (!e2 instanceof DeferredComputationException) throw rethrow(e);
26  
  }
27  
}
28  
29  
sclass DeferredComputation {
30  
  new Flag done;
31  
  O value;
32  
  Throwable error;
33  
  Runnable continuation, onError;
34  
35  
  void setContinuation(Runnable r) {
36  
    synchronized(done) {
37  
      if (!done.isUp()) ret with continuation = r;
38  
    }
39  
    callF(r);
40  
  }
41  
42  
  void setValue(O value) {
43  
    synchronized(done) {
44  
      if (done.isUp()) fail("setValue/setError called twice");
45  
      this.value = value;
46  
      done.raise();
47  
    }
48  
    callF(continuation);
49  
  }
50  
51  
  void setError(Throwable error) {
52  
    synchronized(done) {
53  
      if (done.isUp()) fail("setValue/setError called twice");
54  
      this.error = error;
55  
      done.raise();
56  
    }
57  
    if (onError != null)
58  
      callF(onError);
59  
    else
60  
      printStackTrace(error);
61  
  }
62  
63  
  O getValue() {
64  
    assertTrue(done.isUp());
65  
    ret value;
66  
  }
67  
}
68  
69  
// f1: func -> A, f2 : func(A) -> B, returns: B
70  
static O chainDeferrableComputations(O f1, fO f2) {
71  
  O result;
72  
  try {
73  
    result = callF(f1);
74  
  } catch (RuntimeException e) {
75  
    Throwable e2 = innerException(e);
76  
    if (!e2 instanceof DeferredComputationException) throw rethrow(e);
77  
    final DeferredComputation f = e2/DeferredComputationException.future;
78  
    
79  
    final new DeferredComputation future2;
80  
    f.setContinuation(r {
81  
      try {
82  
        future2.setValue(callF(f2, f.getValue()));
83  
      } catch e {
84  
        future2.setError(e);
85  
      }
86  
    });
87  
    throw DeferredComputationException(future2);
88  
  }
89  
  ret callF(f2, result);
90  
}
91  
92  
static <A> A waitThenCompute(int delay, final F0<A> f) {
93  
  final new DeferredComputation dc;
94  
  doLater(delay, r {
95  
    try {
96  
      dc.setValue(callF(f));
97  
    } catch e {
98  
      dc.setError(e);
99  
    }
100  
  });
101  
  throw DeferredComputationException(dc);
102  
}
103  
104  
sclass DeferredComputationException extends QuickException {
105  
  DeferredComputation future;
106  
107  
  *(DeferredComputation *future) {}
108  
}

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: #1015714
Snippet name: Deferred Computation Spike
Eternal ID of this version: #1015714/6
Text MD5: c8c20ef6f574638aa9b9b96105a1882f
Transpilation MD5: 0e8e26dcaee0e7f9bbb56cdae82d5875
Author: stefan
Category:
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-24 17:43:02
Source code size: 2382 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 378 / 927
Version history: 5 change(s)
Referenced in: [show references]