Created
December 20, 2019 14:29
-
-
Save qingwei91/c1d069300ea202f56e5a45a437c94d6e 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
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