Skip to content

Instantly share code, notes, and snippets.

@qingwei91
Created December 20, 2019 14:29
Show Gist options
  • Save qingwei91/c1d069300ea202f56e5a45a437c94d6e to your computer and use it in GitHub Desktop.
Save qingwei91/c1d069300ea202f56e5a45a437c94d6e to your computer and use it in GitHub Desktop.
def cancellableLoop[F[_], LoopCtx, A](
step: LoopCtx => Either[LoopCtx, A]
)(init: LoopCtx)(implicit cs: ContextShift[F], monad: Monad[F]): F[A] = {
def inner(in: LoopCtx, i: Int): F[A] = {
if (i > 2000) {
cs.shift.flatMap(_ => inner(in, 0))
} else {
step(in) match {
case Left(cont) => inner(cont, i + 1)
case Right(a) => a.pure[F]
}
}
}
inner(init, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment