package extra; import static avian.Continuations.shift; import static avian.Cell.cons; import static avian.Cell.equal; import avian.Cell; import avian.Function; import avian.Continuations; 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 * Continuations.reset(new Callable() { public Integer call() throws Exception { return 1 + shift (new Function,Integer>() { public Integer call(Function continuation) throws Exception { return continuation.call(5); } }); } }) == 12); expect(1 + Continuations.reset(new Callable() { public Integer call() throws Exception { return 2 * shift (new Function,Integer>() { public Integer call(Function continuation) throws Exception { return continuation.call(continuation.call(4)); } }); } }) == 17); expect (equal (Continuations.,Cell>reset (new Callable>() { public Cell call() throws Exception { shift(new Function,Cell>, Cell>() { public Cell call (Function,Cell> continuation) throws Exception { return cons(1, continuation.call(null)); } }); shift(new Function,Cell>, Cell>() { public Cell call (Function,Cell> continuation) throws Exception { return cons(2, continuation.call(null)); } }); return null; } }), cons(1, cons(2, null)))); expect (equal (Continuations.reset (new Callable() { public String call() throws Exception { return new String (shift(new Function,Integer>() { public Integer call(Function continuation) throws Exception { return Integer.parseInt (continuation.call(new byte[] { 0x34, 0x32 })); } }), "UTF-8"); } }), 42)); } }