Skip to content

Instantly share code, notes, and snippets.

@bbjubjub2494
Created February 26, 2020 19:41
Show Gist options
  • Save bbjubjub2494/8eb34a3b091568d594f9625df4a25584 to your computer and use it in GitHub Desktop.
Save bbjubjub2494/8eb34a3b091568d594f9625df4a25584 to your computer and use it in GitHub Desktop.
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