Last active
December 13, 2017 12:19
-
-
Save JoolsF/bdecf44973fdea3645bcbd8bc6e20754 to your computer and use it in GitHub Desktop.
Scalaz FutureValidation retry
This file contains hidden or 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 com.bfil.api.{FutureValidation, TypedFutureValidation} | |
| case class CustomError(msg: String) | |
| object CustomResult extends TypedFutureValidation[CustomError] | |
| def retry[T](attempts: Int, f: => FutureValidation[CustomError, T]): FutureValidation[CustomError, T] = { | |
| def attempt(n: Int): FutureValidation[CustomError, T] = n match { | |
| case n if n > 0 => f.map { | |
| identity | |
| }.leftFlatMap { _ => attempt(n - 1) } | |
| case e => CustomResult.failure(CustomError(s"All retry $attempts attempts failed")) | |
| } | |
| attempt(attempts) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment