From bd7fe87532446cc0ad913a1343a0587eb0739991 Mon Sep 17 00:00:00 2001 From: Tomas Mikula Date: Wed, 9 Mar 2016 20:51:47 -0500 Subject: [PATCH] Make Call.loop @tailrec optimized. --- core/src/main/scala/cats/Eval.scala | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/Eval.scala b/core/src/main/scala/cats/Eval.scala index dfe03f7d1e..867abe42b1 100644 --- a/core/src/main/scala/cats/Eval.scala +++ b/core/src/main/scala/cats/Eval.scala @@ -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) } /**