Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions core/src/main/scala/cats/FlatMapRec.scala
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]
}
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/MonadRec.scala
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]