Skip to content

Instantly share code, notes, and snippets.

@ceedubs
Created October 20, 2015 11:20
Show Gist options
  • Save ceedubs/f869828204ac1116c315 to your computer and use it in GitHub Desktop.
Save ceedubs/f869828204ac1116c315 to your computer and use it in GitHub Desktop.
Port of example code from http://www.scala-lang.org/api/2.10.4/#scala.util.control.TailCalls$ to Trampoline. Note that for this example, Eval is probably more efficient.
import cats.free.Trampoline, Trampoline._
import cats.implicits._
def isEven(xs: List[Int]): Trampoline[Boolean] =
if (xs.isEmpty) done(true) else suspend(isOdd(xs.tail))
def isOdd(xs: List[Int]): Trampoline[Boolean] =
if (xs.isEmpty) done(false) else suspend(isEven(xs.tail))
// returns true (doesn't overflow the stack)
isEven((1 to 100000).toList).run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment