package org.xander.behavioral.lazyevaluation;

public class TailCalls {
    public static <T> TailCall<T> call(final TailCall<T> nextCall) { return nextCall; }
    public static <T> TailCall<T> done(final T value) { 
        return new TailCall<T>() {
            @Override public boolean isComplete() { return true; }
            @Override public T result() { return value; } 
            @Override public TailCall<T> apply() { throw new Error("not implemented"); }
        };
    }
    
    public static void main(String[] args) {
        System.out.println(factorialTailRec(1, 5).invoke());        
        System.out.println(factorialTailRec(1, 20000).invoke());
    }
}

@@ -478,24 +478,301 @@
 %0A    %7D%0A    %0A
+    public static TailCall%3CInteger%3E factorialTailRec( final int factorial, final int number) %7B%0A        if (number == 1) %7B%0A            return done(factorial);%0A        %7D else %7B%0A            return call(() -%3E factorialTailRec(factorial * number, number - 1));%0A        %7D%0A    %7D%0A    %0A
     public s

Update TailCalls.java