mirror of
https://github.com/corda/corda.git
synced 2025-01-06 05:04:20 +00:00
simplify shift/reset API and add test (currently failing)
This commit is contained in:
parent
91e4d2b4a1
commit
aa3fa1aff4
@ -205,11 +205,11 @@ public class Continuations {
|
||||
}
|
||||
}
|
||||
|
||||
public static <Result, Argument> Result shift
|
||||
(final FunctionReceiver<Result, Argument> receiver)
|
||||
public static <T> T shift
|
||||
(final FunctionReceiver<T> receiver)
|
||||
throws Exception
|
||||
{
|
||||
return (Result) callWithCurrentContinuation(new CallbackReceiver() {
|
||||
return (T) callWithCurrentContinuation(new CallbackReceiver() {
|
||||
public Object receive(final Callback continuation) {
|
||||
final Reset reset = latestReset.get();
|
||||
final Callback resetContinuation = reset.continuation;
|
||||
|
@ -10,6 +10,6 @@
|
||||
|
||||
package avian;
|
||||
|
||||
public interface Function<Argument, Result> {
|
||||
public Result call(Argument argument) throws Exception;
|
||||
public interface Function<T> {
|
||||
public T call(T argument) throws Exception;
|
||||
}
|
||||
|
@ -10,6 +10,6 @@
|
||||
|
||||
package avian;
|
||||
|
||||
public interface FunctionReceiver<Argument, Result> {
|
||||
public Result receive(Function<Argument, Result> function) throws Exception;
|
||||
public interface FunctionReceiver<T> {
|
||||
public T receive(Function<T> function) throws Exception;
|
||||
}
|
||||
|
@ -16,9 +16,8 @@ public class ComposableContinuations {
|
||||
public static void main(String[] args) throws Exception {
|
||||
expect(2 * reset(new Callable<Integer>() {
|
||||
public Integer call() throws Exception {
|
||||
return 1 + shift(new FunctionReceiver<Integer, Integer>() {
|
||||
public Integer receive
|
||||
(Function<Integer, Integer> continuation)
|
||||
return 1 + shift(new FunctionReceiver<Integer>() {
|
||||
public Integer receive(Function<Integer> continuation)
|
||||
throws Exception
|
||||
{
|
||||
return continuation.call(5);
|
||||
@ -29,9 +28,8 @@ public class ComposableContinuations {
|
||||
|
||||
expect(1 + reset(new Callable<Integer>() {
|
||||
public Integer call() throws Exception {
|
||||
return 2 * shift(new FunctionReceiver<Integer, Integer>() {
|
||||
public Integer receive
|
||||
(Function<Integer, Integer> continuation)
|
||||
return 2 * shift(new FunctionReceiver<Integer>() {
|
||||
public Integer receive(Function<Integer> continuation)
|
||||
throws Exception
|
||||
{
|
||||
return continuation.call(continuation.call(4));
|
||||
@ -39,5 +37,74 @@ public class ComposableContinuations {
|
||||
});
|
||||
}
|
||||
}) == 17);
|
||||
|
||||
expect(equal(reset(new Callable<Cell<Integer>>() {
|
||||
public Cell<Integer> call() throws Exception {
|
||||
shift(new FunctionReceiver<Cell<Integer>>() {
|
||||
public Cell<Integer> receive
|
||||
(Function<Cell<Integer>> continuation)
|
||||
throws Exception
|
||||
{
|
||||
return cons(1, continuation.call(null));
|
||||
}
|
||||
});
|
||||
|
||||
shift(new FunctionReceiver<Cell<Integer>>() {
|
||||
public Cell<Integer> receive
|
||||
(Function<Cell<Integer>> continuation)
|
||||
throws Exception
|
||||
{
|
||||
return cons(2, continuation.call(null));
|
||||
}
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
}), cons(1, cons(2, null))));
|
||||
}
|
||||
|
||||
private static <T> boolean equal(T a, T b) {
|
||||
return (a == null && b == null) || (a != null && a.equals(b));
|
||||
}
|
||||
|
||||
private static <Car> boolean equal(Cell<Car> a, Cell<Car> b) {
|
||||
System.out.println(a + " vs " + b);
|
||||
|
||||
while (a != null) {
|
||||
if (b == null || (! equal(a.car, b.car))) {
|
||||
return false;
|
||||
}
|
||||
a = a.cdr;
|
||||
b = b.cdr;
|
||||
}
|
||||
|
||||
return b == null;
|
||||
}
|
||||
|
||||
private static <Car> Cell<Car> cons(Car car, Cell<Car> cdr) {
|
||||
return new Cell(car, cdr);
|
||||
}
|
||||
|
||||
private static class Cell<Car> {
|
||||
public final Car car;
|
||||
public final Cell<Car> cdr;
|
||||
|
||||
public Cell(Car car, Cell<Car> cdr) {
|
||||
this.car = car;
|
||||
this.cdr = cdr;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("(");
|
||||
for (Cell<Car> c = this; c != null; c = c.cdr) {
|
||||
sb.append(c.car);
|
||||
if (c.cdr != null) {
|
||||
sb.append(", ");
|
||||
}
|
||||
}
|
||||
sb.append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user