forked from typelevel/cats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request typelevel#1041 from TomasMikula/MonadRec-instances
Some MonadRec instances.
- Loading branch information
Showing
22 changed files
with
333 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cats | ||
|
||
import simulacrum.typeclass | ||
|
||
import cats.data.Xor | ||
|
||
/** | ||
* Version of [[cats.FlatMap]] capable of stack-safe recursive `flatMap`s. | ||
* | ||
* Based on Phil Freeman's | ||
* [[http://functorial.com/stack-safety-for-free/index.pdf Stack Safety for Free]]. | ||
*/ | ||
@typeclass trait FlatMapRec[F[_]] extends FlatMap[F] { | ||
|
||
/** | ||
* Keeps calling `f` until a `[[cats.data.Xor.Right Right]][B]` is returned. | ||
* | ||
* Implementations of this method must use constant stack space. | ||
* | ||
* `f` must use constant stack space. (It is OK to use a constant number of | ||
* `map`s and `flatMap`s inside `f`.) | ||
*/ | ||
def tailRecM[A, B](a: A)(f: A => F[A Xor B]): F[B] | ||
} |
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,5 @@ | ||
package cats | ||
|
||
import simulacrum.typeclass | ||
|
||
@typeclass trait MonadRec[F[_]] extends Monad[F] with FlatMapRec[F] |
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
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 laws | ||
|
||
import cats.data.Xor | ||
import cats.syntax.flatMap._ | ||
import cats.syntax.functor._ | ||
|
||
/** | ||
* Laws that must be obeyed by any `FlatMapRec`. | ||
*/ | ||
trait FlatMapRecLaws[F[_]] extends FlatMapLaws[F] { | ||
implicit override def F: FlatMapRec[F] | ||
|
||
def tailRecMConsistentFlatMap[A](a: A, f: A => F[A]): IsEq[F[A]] = { | ||
val bounce = F.tailRecM[(A, Int), A]((a, 1)) { case (a0, i) => | ||
if(i > 0) f(a0).map(a1 => Xor.left((a1, i-1))) | ||
else f(a0).map(Xor.right) | ||
} | ||
bounce <-> f(a).flatMap(f) | ||
} | ||
} | ||
|
||
object FlatMapRecLaws { | ||
def apply[F[_]](implicit ev: FlatMapRec[F]): FlatMapRecLaws[F] = | ||
new FlatMapRecLaws[F] { def F: FlatMapRec[F] = ev } | ||
} |
Oops, something went wrong.