June 2018
Intermediate to advanced
280 pages
7h 46m
English
John McClean, the maintainer of cyclops-react, demos the usage of TCO that calculates numbers in the Fibonacci sequence at https://gist.github.com/johnmcclean/fb1735b49e6206396bd5792ca11ba7b2. The code is clean and simple to understand; it basically accumulates Fibonacci numbers starting from the initial states—a and b, f(0) = 0, f(1) = 1—and applying the f(n) = f(n-1) + f(n-2) functions:
importstatic cyclops.control.Trampoline.done;importstatic cyclops.control.Trampoline.more;import cyclops.control.Trampoline;publicclass Main { publicvoid 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 ...Read now
Unlock full access