Skip to content

Instantly share code, notes, and snippets.

@JoolsF
Last active December 13, 2017 12:19
Show Gist options
  • Select an option

  • Save JoolsF/bdecf44973fdea3645bcbd8bc6e20754 to your computer and use it in GitHub Desktop.

Select an option

Save JoolsF/bdecf44973fdea3645bcbd8bc6e20754 to your computer and use it in GitHub Desktop.
Scalaz FutureValidation retry
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