Methods exported by this object implement tail calls via trampolining.
Tail calling methods must either return their result usingdone or call the next method usingtailcall. Both return an instance ofTailRec. The result of evaluating a tailcalling function can be retrieved from aTailRec value using methodresult.
Implemented as described in "Stackless Scala with Free Monads"https://blog.higher-order.com/assets/trampolines.pdf
Here's a usage example:
import scala.util.control.TailCalls._def isEven(xs: List[Int]): TailRec[Boolean] = if (xs.isEmpty) done(true) else tailcall(isOdd(xs.tail))def isOdd(xs: List[Int]): TailRec[Boolean] = if (xs.isEmpty) done(false) else tailcall(isEven(xs.tail))isEven((1 to 100000).toList).resultdef fib(n: Int): TailRec[Int] = if (n < 2) done(n) else for { x <- tailcall(fib(n - 1)) y <- tailcall(fib(n - 2)) } yield x + yfib(40).resultThis class represents a tailcalling computation.
Return the final result from a tailcalling computation.
Return the final result from a tailcalling computation.
the result value
aTailRec object representing a computation which immediately returnsresult
Perform a tailcall.
Perform a tailcall.
the expression to be evaluated in the tailcall
aTailRec object representing the expressionrest