Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @FunctionalInterface
- public interface Function2<A1, A2, R> {
- R apply(A1 a1, A2 a2);
- }
- // first solution
- class Run {
- public static class Self<T> {
- public final Function2<Self<T>, T, T> func;
- public Self(Function2<Self<T>, T, T> func) {
- this.func = func;
- }
- }
- public static final Function<Long, Long> factorial = n -> {
- final Self<Long> trampoline = new Self<>((cont, m) -> {
- if (m == 0L)
- return 1L;
- else
- return m * cont.func.apply(cont, m - 1);
- });
- return trampoline.func.apply(trampoline, n);
- };
- public static void main(String... args) {
- println("factorial(10) = " + factorial.apply(10L));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement