Skip to content

Commit

Permalink
Optimize FreeApplicative.product (#1921)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterneyens authored and kailuowang committed Sep 21, 2017
1 parent 5232087 commit 664cb62
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions free/src/main/scala/cats/free/FreeApplicative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ sealed abstract class FreeApplicative[F[_], A] extends Product with Serializable
}
}

final def map2[B, C](fb: FA[F, B])(f: (A, B) => C): FA[F, C] =
this match {
case Pure(a) => fb.map(f(a, _))
case _ =>
fb match {
case Pure(b) => Ap(Pure(f((_: A), b)), this)
case _ => Ap(Ap(Pure((a: A) => (b: B) => f(a, b)), this), fb)
}
}

/** Interprets/Runs the sequence of operations using the semantics of `Applicative` G[_].
* Tail recursive.
*/
Expand Down Expand Up @@ -193,16 +203,19 @@ object FreeApplicative {
final def lift[F[_], A](fa: F[A]): FA[F, A] =
Lift(fa)

implicit final def freeApplicative[S[_]]: Applicative[FA[S, ?]] = {
implicit final def freeApplicative[S[_]]: Applicative[FA[S, ?]] =
new Applicative[FA[S, ?]] {
override def product[A, B](fa: FA[S, A], fb: FA[S, B]): FA[S, (A, B)] = ap(fa.map((a: A) => (b: B) => (a, b)))(fb)
override def product[A, B](fa: FA[S, A], fb: FA[S, B]): FA[S, (A, B)] =
map2(fa, fb)((_, _))

override def map[A, B](fa: FA[S, A])(f: A => B): FA[S, B] = fa.map(f)

override def ap[A, B](f: FA[S, A => B])(fa: FA[S, A]): FA[S, B] = fa.ap(f)

def pure[A](a: A): FA[S, A] = Pure(a)

override def map2[A, B, Z](fa: FA[S, A], fb: FA[S, B])(f: (A, B) => Z): FA[S, Z] =
fa.map2(fb)(f)
}
}

}

0 comments on commit 664cb62

Please sign in to comment.