-
-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
8227961
commit 8e50b78
Showing
6 changed files
with
67 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cats.instances | ||
|
||
import cats.{Defer, StackSafeMonad} | ||
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,29 @@ | ||
package cats.tests | ||
|
||
import cats.{Defer, Eq, Monad} | ||
import cats.laws.discipline.{DeferTests, MonadTests, SerializableTests} | ||
import org.scalacheck.Arbitrary.arbitrary | ||
import org.scalacheck.{Arbitrary, Cogen, Gen} | ||
|
||
import scala.util.control.TailCalls.{done, tailcall, TailRec} | ||
|
||
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])) | ||
} |