Skip to content

Commit

Permalink
Optimize Apply methods in FlatMap (#2597)
Browse files Browse the repository at this point in the history
* optimize productL/R in FlatMap

* slightly optimized ap2 and map2 as well

* reorg

* fmt
  • Loading branch information
kailuowang authored Nov 9, 2018
1 parent 00f2a5f commit 97dc0c2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/src/main/scala/cats/FlatMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ import simulacrum.noop
override def product[A, B](fa: F[A], fb: F[B]): F[(A, B)] =
flatMap(fa)(a => map(fb)(b => (a, b)))

override def ap2[A, B, Z](ff: F[(A, B) => Z])(fa: F[A], fb: F[B]): F[Z] =
flatMap(fa)(a => flatMap(fb)(b => map(ff)(_(a, b))))

override def map2[A, B, Z](fa: F[A], fb: F[B])(f: (A, B) => Z): F[Z] =
flatMap(fa)(a => map(fb)(b => f(a, b)))

override def productR[A, B](fa: F[A])(fb: F[B]): F[B] =
flatMap(fa)(_ => fb)

override def productL[A, B](fa: F[A])(fb: F[B]): F[A] =
map2(fa, fb)((a, _) => a)

/**
* Pair `A` with the result of function application.
*
Expand Down

0 comments on commit 97dc0c2

Please sign in to comment.