Skip to content

Instantly share code, notes, and snippets.

@johnmcclean
Created November 13, 2016 21:18
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 johnmcclean/fb1735b49e6206396bd5792ca11ba7b2 to your computer and use it in GitHub Desktop.
Save johnmcclean/fb1735b49e6206396bd5792ca11ba7b2 to your computer and use it in GitHub Desktop.
Tail call optimized / stackless recursion fibonacci sequence in Java 8 using cyclops-react
import static com.aol.cyclops.control.Trampoline.done;
import static com.aol.cyclops.control.Trampoline.more;
public void fib() {
for(int i=0;i<100_000;i++)
System.out.println(fibonacci(i, 0l, 1l).get());
}
public Trampoline<Long> fibonacci(Integer count, Long a, Long b) {
return count==0 ? done(a) : more(()->fibonacci (count - 1, b, a + b));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment