Skip to content

Commit 8a87d55

Browse files
committed
Add FlatMapRec, MonadRec.
1 parent 22f2000 commit 8a87d55

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cats
2+
3+
import simulacrum.typeclass
4+
5+
import cats.data.Xor
6+
7+
/**
8+
* Version of [[cats.FlatMap]] capable of stack-safe recursive `flatMap`s.
9+
*
10+
* Based on Phil Freeman's
11+
* [[http://functorial.com/stack-safety-for-free/index.pdf Stack Safety for Free]].
12+
*/
13+
@typeclass trait FlatMapRec[F[_]] extends FlatMap[F] {
14+
15+
/**
16+
* Keeps calling `f` until a `[[cats.data.Xor.Right Right]][B]` is returned.
17+
*
18+
* Implementations of this method must use constant stack space.
19+
*
20+
* `f` must use constant stack space. (It is OK to use a constant number of
21+
* `map`s and `flatMap`s inside `f`.)
22+
*/
23+
def tailRecM[A, B](a: A)(f: A => F[A Xor B]): F[B]
24+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cats
2+
3+
import simulacrum.typeclass
4+
5+
@typeclass trait MonadRec[F[_]] extends Monad[F] with FlatMapRec[F]

0 commit comments

Comments
 (0)