Skip to content

Instantly share code, notes, and snippets.

@fsarradin
Created July 23, 2015 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsarradin/b673a5359baf6052d69c to your computer and use it in GitHub Desktop.
Save fsarradin/b673a5359baf6052d69c to your computer and use it in GitHub Desktop.
import java.util.function.Function;
public interface Continuation<R, A> extends Function<Function<A, R>, R> {
@Override
R apply(Function<A, R> f);
default <B> Continuation<R, B> map(Function<A, B> f) {
return continuation(g -> apply(g.compose(f)));
}
default <B> Continuation<R, B> flatmap(Function<A, Continuation<R, B>> f) {
return continuation(g -> apply(a -> f.apply(a).apply(g)));
}
static <R, A> Continuation<R, A> of(A a) {
return f -> f.apply(a);
}
static <R, A> Continuation<R, A> continuation(Function<Function<A, R>, R> g) {
return g::apply;
}
static <R, A, B> Continuation<R, A> callcc(Function<Function<A, Continuation<R, B>>, Continuation<R, A>> f) {
return continuation(k -> f.apply(a -> continuation(__ -> k.apply(a))).apply(k));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment