Created
October 20, 2015 11:20
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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