-
-
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.
- Loading branch information
Showing
5 changed files
with
24 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cats | ||
|
||
import scala.util.{Either, Left, Right} | ||
|
||
/** | ||
* A mix-in for inheriting tailRecM on monads which define a stack-safe flatMap. This is | ||
* ''not'' an appropriate trait to use unless you are 100% certain your monad is stack-safe | ||
* by definition! If your monad is not stack-safe, then the tailRecM implementation you | ||
* will inherit will not be sound, and will result in unexpected stack overflows. This | ||
* trait is only provided because a large number of monads ''do'' define a stack-safe | ||
* flatMap, and so this particular implementation was being repeated over and over again. | ||
*/ | ||
trait StackSafeMonad[F[_]] extends Monad[F] { | ||
|
||
override def tailRecM[A, B](a: A)(f: A => F[Either[A, B]]): F[B] = flatMap(f(a)) { | ||
case Left(a) => tailRecM(a)(f) | ||
case Right(b) => pure(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
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