mirror of
https://github.com/corda/corda.git
synced 2025-01-08 14:03:06 +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
|
public static <T> T shift
|
||||||
(final FunctionReceiver<Result, Argument> receiver)
|
(final FunctionReceiver<T> receiver)
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
return (Result) callWithCurrentContinuation(new CallbackReceiver() {
|
return (T) callWithCurrentContinuation(new CallbackReceiver() {
|
||||||
public Object receive(final Callback continuation) {
|
public Object receive(final Callback continuation) {
|
||||||
final Reset reset = latestReset.get();
|
final Reset reset = latestReset.get();
|
||||||
final Callback resetContinuation = reset.continuation;
|
final Callback resetContinuation = reset.continuation;
|
||||||
|
@ -10,6 +10,6 @@
|
|||||||
|
|
||||||
package avian;
|
package avian;
|
||||||
|
|
||||||
public interface Function<Argument, Result> {
|
public interface Function<T> {
|
||||||
public Result call(Argument argument) throws Exception;
|
public T call(T argument) throws Exception;
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,6 @@
|
|||||||
|
|
||||||
package avian;
|
package avian;
|
||||||
|
|
||||||
public interface FunctionReceiver<Argument, Result> {
|
public interface FunctionReceiver<T> {
|
||||||
public Result receive(Function<Argument, Result> function) throws Exception;
|
public T receive(Function<T> function) throws Exception;
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,8 @@ public class ComposableContinuations {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
expect(2 * reset(new Callable<Integer>() {
|
expect(2 * reset(new Callable<Integer>() {
|
||||||
public Integer call() throws Exception {
|
public Integer call() throws Exception {
|
||||||
return 1 + shift(new FunctionReceiver<Integer, Integer>() {
|
return 1 + shift(new FunctionReceiver<Integer>() {
|
||||||
public Integer receive
|
public Integer receive(Function<Integer> continuation)
|
||||||
(Function<Integer, Integer> continuation)
|
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
return continuation.call(5);
|
return continuation.call(5);
|
||||||
@ -29,9 +28,8 @@ public class ComposableContinuations {
|
|||||||
|
|
||||||
expect(1 + reset(new Callable<Integer>() {
|
expect(1 + reset(new Callable<Integer>() {
|
||||||
public Integer call() throws Exception {
|
public Integer call() throws Exception {
|
||||||
return 2 * shift(new FunctionReceiver<Integer, Integer>() {
|
return 2 * shift(new FunctionReceiver<Integer>() {
|
||||||
public Integer receive
|
public Integer receive(Function<Integer> continuation)
|
||||||
(Function<Integer, Integer> continuation)
|
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
return continuation.call(continuation.call(4));
|
return continuation.call(continuation.call(4));
|
||||||
@ -39,5 +37,74 @@ public class ComposableContinuations {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}) == 17);
|
}) == 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