From ff7af34a39a2125f06ec5e95bb3daf6b8d9eb652 Mon Sep 17 00:00:00 2001 From: Tomas Mikula Date: Fri, 3 Jun 2016 20:28:09 -0400 Subject: [PATCH] Optimize composed function evaluation. --- core/src/main/scala/cats/data/StateT.scala | 26 +++++----------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/core/src/main/scala/cats/data/StateT.scala b/core/src/main/scala/cats/data/StateT.scala index fb10a71879..4998d3a7d9 100644 --- a/core/src/main/scala/cats/data/StateT.scala +++ b/core/src/main/scala/cats/data/StateT.scala @@ -213,7 +213,7 @@ private[data] sealed trait StateTMonadRec[F[_], S] extends MonadRec[StateT[F, S, private[data] sealed abstract class Fun[A, B] { import Fun._ - def apply(a: A): B = Evaluator(a, this).eval + def apply(a: A): B = eval(a, this.asInstanceOf[Fun[Any, B]]) def andThen[C](g: Fun[B, C]): Fun[A, C] = AndThen(this, g) def andThen[C](g: B => C): Fun[A, C] = andThen(Wrap(g)) } @@ -224,24 +224,10 @@ private[data] object Fun { final case class Wrap[A, B](f: A => B) extends Fun[A, B] final case class AndThen[A, B, C](f: Fun[A, B], g: Fun[B, C]) extends Fun[A, C] - private abstract class Evaluator[B] { - type A - val a: A - val fun: Fun[A, B] - - @annotation.tailrec - final def eval: B = fun match { - case Wrap(f) => f(a) - case AndThen(Wrap(f), g) => Evaluator(f(a), g).eval - case AndThen(AndThen(f, g), h) => Evaluator(a, f andThen (g andThen h)).eval - } - } - - private object Evaluator { - def apply[A0, B](a0: A0, f: Fun[A0, B]): Evaluator[B] = new Evaluator[B] { - type A = A0 - val a = a0 - val fun = f - } + @annotation.tailrec + private final def eval[B](a: Any, fun: Fun[Any, B]): B = fun match { + case Wrap(f) => f(a) + case AndThen(Wrap(f), g) => eval(f(a), g) + case AndThen(AndThen(f, g), h) => eval(a, f andThen (g andThen h)) } }