-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scala.util.control.TailCalls.TailRec instances (#3041)
* Add scala.util.control.TailCalls.TailRec instances * format * review comments * fix conflict * Format * Avoid val in trait for bincompat
- Loading branch information
Showing
8 changed files
with
69 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cats | ||
package instances | ||
|
||
import scala.util.control.TailCalls.{done, tailcall, TailRec} | ||
|
||
trait TailRecInstances { | ||
implicit def catsInstancesForTailRec: StackSafeMonad[TailRec] with Defer[TailRec] = | ||
TailRecInstances.catsInstancesForTailRec | ||
} | ||
|
||
private object TailRecInstances { | ||
val catsInstancesForTailRec: StackSafeMonad[TailRec] with Defer[TailRec] = | ||
new StackSafeMonad[TailRec] with Defer[TailRec] { | ||
def defer[A](fa: => TailRec[A]): TailRec[A] = tailcall(fa) | ||
|
||
def pure[A](a: A): TailRec[A] = done(a) | ||
|
||
override def map[A, B](fa: TailRec[A])(f: A => B): TailRec[B] = | ||
fa.map(f) | ||
|
||
def flatMap[A, B](fa: TailRec[A])(f: A => TailRec[B]): TailRec[B] = | ||
fa.flatMap(f) | ||
|
||
override val unit: TailRec[Unit] = done(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cats | ||
package tests | ||
|
||
import scala.util.control.TailCalls.{done, tailcall, TailRec} | ||
import org.scalacheck.{Arbitrary, Cogen, Gen} | ||
|
||
import Arbitrary.arbitrary | ||
|
||
import cats.laws.discipline.{DeferTests, MonadTests, SerializableTests} | ||
|
||
class TailRecSuite extends CatsSuite { | ||
|
||
implicit def tailRecArb[A: Arbitrary: Cogen]: Arbitrary[TailRec[A]] = | ||
Arbitrary( | ||
Gen.frequency( | ||
(3, arbitrary[A].map(done(_))), | ||
(1, Gen.lzy(arbitrary[(A, A => TailRec[A])].map { case (a, fn) => tailcall(fn(a)) })), | ||
(1, Gen.lzy(arbitrary[(TailRec[A], A => TailRec[A])].map { case (a, fn) => a.flatMap(fn) })) | ||
) | ||
) | ||
|
||
implicit def eqTailRec[A: Eq]: Eq[TailRec[A]] = | ||
Eq.by[TailRec[A], A](_.result) | ||
|
||
checkAll("TailRec[Int]", MonadTests[TailRec].monad[Int, Int, Int]) | ||
checkAll("Monad[TailRec]", SerializableTests.serializable(Monad[TailRec])) | ||
|
||
checkAll("TailRec[Int]", DeferTests[TailRec].defer[Int]) | ||
checkAll("Defer[TailRec]", SerializableTests.serializable(Defer[TailRec])) | ||
} |