Created
July 23, 2015 19:19
-
-
Save fsarradin/b673a5359baf6052d69c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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