Skip to content

Instantly share code, notes, and snippets.

@youngnh
Created August 26, 2013 20:48
Show Gist options
  • Save youngnh/6346490 to your computer and use it in GitHub Desktop.
Save youngnh/6346490 to your computer and use it in GitHub Desktop.
CPS Combinators in Java
public class Next<A, B, C> extends Task<A, C> {
private Task<A, B> task;
private Task<B, C> nextTask;
public Next(Task<A, B> task, Task<B, C> nextTask) {
this.task = task;
this.nextTask = nextTask;
}
@Override
public <D> D call(A arg, final Callable1<C, D> after) throws Exception {
return this.task.call(arg, new Callable1<B, D>() {
@Override
public D call(B nextArg) throws Exception {
return Next.this.nextTask.call(nextArg, after);
}
});
}
}
public class Again<A, B, C> extends Task<A, Pair<B, C>> {
private Task<A, B> task;
private Task<A, C> nextTask;
public Again(Task<A, B> task, Task<A, C> nextTask) {
this.task = task;
this.nextTask = nextTask;
}
@Override
public <D> D call(final A arg, final Callable1<Pair<B, C>, D> after) throws Exception {
return this.task.call(arg, new Callable1<B, D>() {
@Override
public D call(final B result1) throws Exception {
return Again.this.nextTask.call(arg, new Callable1<C, D>() {
@Override
public D call(C result2) throws Exception {
return after.call(new Pair(result1, result2));
}
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment