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

!7

p-experiment {
  allowDeferredComputation(r {
    chainDeferrableComputations(f serveLater, fg(f print, f reversedString));
  });
  allowDeferredComputation(r {
    chainDeferrableComputations(f serveNow, fg(f print, f reversedString));
  });
}

sS serveNow() {
  ret "Served now";
}

sS serveLater() {
  ret waitThenCompute(1000, func -> S { "Served late" });
}

svoid allowDeferredComputation(Runnable r) {
  try {
    callF(r);
  } catch (RuntimeException e) {
    Throwable e2 = innerException(e);
    if (!e2 instanceof DeferredComputationException) throw rethrow(e);
  }
}

sclass DeferredComputation {
  new Flag done;
  O value;
  Throwable error;
  Runnable continuation, onError;

  void setContinuation(Runnable r) {
    synchronized(done) {
      if (!done.isUp()) ret with continuation = r;
    }
    callF(r);
  }

  void setValue(O value) {
    synchronized(done) {
      if (done.isUp()) fail("setValue/setError called twice");
      this.value = value;
      done.raise();
    }
    callF(continuation);
  }

  void setError(Throwable error) {
    synchronized(done) {
      if (done.isUp()) fail("setValue/setError called twice");
      this.error = error;
      done.raise();
    }
    if (onError != null)
      callF(onError);
    else
      printStackTrace(error);
  }

  O getValue() {
    assertTrue(done.isUp());
    ret value;
  }
}

// f1: func -> A, f2 : func(A) -> B, returns: B
static O chainDeferrableComputations(O f1, fO f2) {
  O result;
  try {
    result = callF(f1);
  } catch (RuntimeException e) {
    Throwable e2 = innerException(e);
    if (!e2 instanceof DeferredComputationException) throw rethrow(e);
    final DeferredComputation f = e2/DeferredComputationException.future;
    
    final new DeferredComputation future2;
    f.setContinuation(r {
      try {
        future2.setValue(callF(f2, f.getValue()));
      } catch e {
        future2.setError(e);
      }
    });
    throw DeferredComputationException(future2);
  }
  ret callF(f2, result);
}

static <A> A waitThenCompute(int delay, final F0<A> f) {
  final new DeferredComputation dc;
  doLater(delay, r {
    try {
      dc.setValue(callF(f));
    } catch e {
      dc.setError(e);
    }
  });
  throw DeferredComputationException(dc);
}

sclass DeferredComputationException extends QuickException {
  DeferredComputation future;

  *(DeferredComputation *future) {}
}

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: 376 / 920
Version history: 5 change(s)
Referenced in: [show references]