Skip to content

Commit

Permalink
Merge pull request #923 from TomasMikula/trailrec-eval
Browse files Browse the repository at this point in the history
Make Call.loop @tailrec optimized.
  • Loading branch information
stew committed Mar 16, 2016
2 parents 92a3c5c + bd7fe87 commit 3f48848
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions core/src/main/scala/cats/Eval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,23 @@ object Eval extends EvalInstances {

object Call {
/** Collapse the call stack for eager evaluations */
private def loop[A](fa: Eval[A]): Eval[A] = fa match {
@tailrec private def loop[A](fa: Eval[A]): Eval[A] = fa match {
case call: Eval.Call[A] =>
loop(call.thunk())
case compute: Eval.Compute[A] =>
new Eval.Compute[A] {
type Start = compute.Start
val start: () => Eval[Start] = () => compute.start()
val run: Start => Eval[A] = s => loop(compute.run(s))
val run: Start => Eval[A] = s => loop1(compute.run(s))
}
case other => other
}

/**
* Alias for loop that can be called in a non-tail position
* from an otherwise tailrec-optimized loop.
*/
private def loop1[A](fa: Eval[A]): Eval[A] = loop(fa)
}

/**
Expand Down

0 comments on commit 3f48848

Please sign in to comment.