mirror of
https://github.com/corda/corda.git
synced 2025-01-22 12:28:11 +00:00
91e4d2b4a1
I've been told by knowledgeable people that it is impossible to implement composable continuations (AKA delimited continuations AKA shift/reset) in terms of call-with-current-continuation. Since I don't yet understand why that is, I figured it would help my understanding to attempt it and see how it fails.
44 lines
1.3 KiB
Java
44 lines
1.3 KiB
Java
package extra;
|
|
|
|
import static avian.Continuations.shift;
|
|
import static avian.Continuations.reset;
|
|
|
|
import avian.FunctionReceiver;
|
|
import avian.Function;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
public class ComposableContinuations {
|
|
private static void expect(boolean v) {
|
|
if (! v) throw new RuntimeException();
|
|
}
|
|
|
|
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)
|
|
throws Exception
|
|
{
|
|
return continuation.call(5);
|
|
}
|
|
});
|
|
}
|
|
}) == 12);
|
|
|
|
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)
|
|
throws Exception
|
|
{
|
|
return continuation.call(continuation.call(4));
|
|
}
|
|
});
|
|
}
|
|
}) == 17);
|
|
}
|
|
}
|