Packages

object TailCalls

Methods exported by this object implement tail calls via trampolining. Tail calling methods have to return their result using done or call the next method using tailcall. Both return a TailRec object. The result of evaluating a tailcalling function can be retrieved from a Tailrec value using method result. Implemented as described in "Stackless Scala with Free Monads" http://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).result

def fib(n: Int): TailRec[Int] =
  if (n < 2) done(n) else for {
    x <- tailcall(fib(n - 1))
    y <- tailcall(fib(n - 2))
  } yield (x + y)

fib(40).result
Source
TailCalls.scala
Linear Supertypes
Content Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TailCalls
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. abstract class TailRec[+A] extends AnyRef

    This class represents a tailcalling computation

Value Members

  1. def done[A](result: A): TailRec[A]

    Used to return final result from tailcalling computation

    Used to return final result from tailcalling computation

    returns

    a TailRec object representing a computation which immediately returns result

  2. def tailcall[A](rest: => TailRec[A]): TailRec[A]

    Performs a tailcall

    Performs a tailcall

    rest

    the expression to be evaluated in the tailcall

    returns

    a TailRec object representing the expression rest