Created
February 26, 2020 19:41
-
-
Save bbjubjub2494/8eb34a3b091568d594f9625df4a25584 to your computer and use it in GitHub Desktop.
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
import $ivy.`org.typelevel::cats-core:2.1.0` | |
import cats._ | |
import data.ContT | |
import syntax.all._ | |
// ContT.liftF, currently unreleased. | |
def ContT_liftF[M[_]: FlatMap, R, A](ma: M[A]): ContT[M, R, A] = | |
ContT(ma.flatMap(_)) | |
// Is this stack safe? | |
def callCC[M[_]: Monad, R, A](f: (M[A] => ContT[M, R, Nothing]) => ContT[M, R, A]): ContT[M, R, A] = | |
ContT(cont => f(ContT_liftF(_).withCont(_ => cont)).run(cont)) | |
// Example use | |
def getCC[M[_]: Monad: Defer, R]: ContT[M, R, ContT[M, R, Nothing]] = { | |
callCC[M, R, ContT[M, R, Nothing]] { | |
type F[A] = ContT[M, R, A] | |
escape => Defer[F].fix[Nothing](self => escape(Monad[M].pure(self))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment